Forums

Just `base`

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Just `base`

Postby Charles » Tue Jan 21, 2014 3:54 am

As with many object-oriented languages, it's common enough in Cobra to invoke a base method (aka "super" in some languages). For example:
class A inherits B

def doSomething(name as String, count as int)
base.doSomething(name, count - 1)
# do more stuff

Invoking the same method name is extremely common and passing the same parameters is fairly common as well. I'm considering allowing two syntactic shortcuts. One is to allow the method name to be excluded:
class A inherits B

def doSomething(name as String, count as int)
base(name, count - 1)
# do more stuff

And the other is to allow the arguments to be excluded, presuming you want to pass them as they were passed in with no changes:
class A inherits B

def doSomething(name as String, count as int)
base
# do more stuff

`base` is already a keyword btw.

I can't think of any conflicts or problems that this would lead to? Can you? Any objections, comments, opinions?

Are there other languages that allow a shortened form of base/super invocation?
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Just `base`

Postby nerdzero » Tue Jan 21, 2014 12:07 pm

That could be handy. Would it generate a warning if you were explicit and didn't need to be like with extra parenthesis or a colon? Just thinking about any legacy code that used to compile without issue that would now generate warnings. Not a big deal either way, but something to consider.
nerdzero
 
Posts: 286
Location: Chicago, IL

Re: Just `base`

Postby oahmad04 » Wed Jan 22, 2014 11:21 am

What if you don't want to pass any arguments to base? What would that look like?
oahmad04
 
Posts: 19

Re: Just `base`

Postby Charles » Wed Jan 22, 2014 11:58 am

@nerdzero, I would baby step it. First, just the feature. If we still like it then an option to turn on the warnings. Later the default would be on and the option would then be used to turn off the warnings.

@oahmad04, this is covered in my first post:
base(name, count - 1)

This is the same syntax used to call a delegate/method reference.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Just `base`

Postby oahmad04 » Wed Jan 22, 2014 12:18 pm

Charles wrote:@oahmad04, this is covered in my first post:
base(name, count - 1)

This is the same syntax used to call a delegate/method reference.

I meant what if you don't want to pass "name" and "count" to the base method. For example:
cue init(name, count)

# where this base call does not receive name or
# count as parameters
base

If your response answers my question, I'm sorry, but I don't understand it.
oahmad04
 
Posts: 19

Re: Just `base`

Postby oahmad04 » Wed Jan 22, 2014 12:26 pm

@Charles In your response you were passing different arguments (count is modified, 1 is subtracted). I was asking what you would do if you don't want any arguments passed whatsoever.
oahmad04
 
Posts: 19

Re: Just `base`

Postby nerdzero » Wed Jan 22, 2014 3:17 pm

My understanding is that in that case since you are calling a different overload than the one in the current context, you couldn't just use 'base' and would have to do 'base.init' like this:

cue init(name, count)
base.init
nerdzero
 
Posts: 286
Location: Chicago, IL

Re: Just `base`

Postby oahmad04 » Wed Jan 22, 2014 7:16 pm

nerdzero wrote:My understanding is that in that case since you are calling a different overload than the one in the current context, you couldn't just use 'base' and would have to do 'base.init' like this:

cue init(name, count)
base.init

Oh, I see. It's a "different overload" because the parameter types (or in this case number of parameters) are different, right? Sorry, I'm still a student and I've mostly only programmed in Python (we're just learning C now). I'm not used to the idea that you can have different methods with the same name as long as they have different types.
oahmad04
 
Posts: 19

Re: Just `base`

Postby kirai84 » Thu Jan 23, 2014 5:17 am

What about methods with default parameters?
class ClassA
def someMethod(i as int = 0)
...

class ClassB
inherits classA
def someMethod(i as int = 0) is override
base # will it be base(0) or base(1) in the call below?

...
o = ClassB()
o.someMethod(1)

Also a code like the following can be confusing
def someMethod(i as int)
i = 2
base # will it be base(1) or base(2) in the call below

...
o.someMethod(1)

Imho bare base call with implicit arguments is not a good idea. While bare base call (with explicit or default arguments) is a good one.
kirai84
 
Posts: 24

Re: Just `base`

Postby nerdzero » Thu Jan 23, 2014 9:23 am

oahmad04 wrote:It's a "different overload" because the parameter types (or in this case number of parameters) are different, right?

Correct. You can learn more about it overloading here: https://en.wikipedia.org/wiki/Function_overloading

@kirai84, interesting examples. I had to think about it for a bit, but if the intent is that "base" is just shorthand for the explicit call I would expect 'base.someMethod(1)' to be called in the first example and 'base.someMethod(2)' in the second example. Would that be right, Charles?
nerdzero
 
Posts: 286
Location: Chicago, IL

Next

Return to Discussion

Who is online

Users browsing this forum: No registered users and 32 guests