Forums

Optional Parameters

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

Optional Parameters

Postby nerdzero » Tue Sep 04, 2012 7:54 pm

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.
nerdzero
 
Posts: 286
Location: Chicago, IL

Re: Optional Parameters

Postby Charles » Wed Sep 05, 2012 12:36 am

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.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Optional Parameters

Postby nerdzero » Wed Sep 05, 2012 8:10 am

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.
nerdzero
 
Posts: 286
Location: Chicago, IL

Re: Optional Parameters

Postby Charles » Wed Sep 05, 2012 12:37 pm

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
Charles
 
Posts: 2515
Location: Los Angeles, CA


Return to Discussion

Who is online

Users browsing this forum: No registered users and 9 guests

cron