Page 1 of 2

Just `base`

PostPosted: Tue Jan 21, 2014 3:54 am
by Charles
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?

Re: Just `base`

PostPosted: Tue Jan 21, 2014 12:07 pm
by nerdzero
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.

Re: Just `base`

PostPosted: Wed Jan 22, 2014 11:21 am
by oahmad04
What if you don't want to pass any arguments to base? What would that look like?

Re: Just `base`

PostPosted: Wed Jan 22, 2014 11:58 am
by Charles
@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.

Re: Just `base`

PostPosted: Wed Jan 22, 2014 12:18 pm
by oahmad04
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.

Re: Just `base`

PostPosted: Wed Jan 22, 2014 12:26 pm
by oahmad04
@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.

Re: Just `base`

PostPosted: Wed Jan 22, 2014 3:17 pm
by nerdzero
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

Re: Just `base`

PostPosted: Wed Jan 22, 2014 7:16 pm
by oahmad04
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.

Re: Just `base`

PostPosted: Thu Jan 23, 2014 5:17 am
by kirai84
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.

Re: Just `base`

PostPosted: Thu Jan 23, 2014 9:23 am
by nerdzero
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?