Page 1 of 1

Optional Parameters

PostPosted: Tue Sep 04, 2012 7:54 pm
by nerdzero
Brain cramp. What's the syntax for optional parameters? I can't seem to find it despite my searching. This doesn't compile:

#Cobra svn:2790 (post 0.8) / 2012-09-01 on Mono 2.10.8.1 CLR v4.0.30319 on Ubuntu 12.04.1 LTS
class PrintDefault
def main
.foo
.foo(false)

def foo(bar=true)
print "[bar]"

Code: Select all
error: Expecting COMMA, but got "=" (ASSIGN) instead.

I've been writing code all day so maybe I just need another pair of eyes.

Re: Optional Parameters

PostPosted: Wed Sep 05, 2012 12:36 am
by Charles
There is none currently. The work around is to use overloads:
class Foo

def foo
.foo(true)

def foo(bar as bool)
pass

We'll have them someday, but since the workaround is fairly easy, they are not a high priority.

Re: Optional Parameters

PostPosted: Wed Sep 05, 2012 8:10 am
by nerdzero
I know why I thought they were. Back when I was looking at the code for the InExpr class I saw some similar syntax. I just checked and found this on line 824 of BinaryOpExr.cobra.

contains = DotExpr(.token, 'DOT', _right, CallExpr(.token, 'contains', List<of Expr>([_left]), true), isImplicit=true)

I remembered seeing the isImplicit=true somewhere but didn't remember the context. I'll use an overload then. Thanks.

Re: Optional Parameters

PostPosted: Wed Sep 05, 2012 12:37 pm
by Charles
When you instantiate a class, you can initialize properties:
class Foo

pro a from var as String

pro b from var as int

...

f = Foo(a='bar', b=3)

# instead of:
f = Foo()
f.a = 'bar'
f.b = 3