Page 1 of 1

Type casting ?

PostPosted: Sun Apr 29, 2012 10:28 am
by berseker59
Hi, is there a casting operator in cobra ?

At the moment, i think i have to do this to pass two ChildClass object to the doSomethingSpecial method
def foo(a as ParentClass, b as ParentClass)
if a inherits ChildClass
if b inherits ChildClass
.doSomethingSpecial(a, b)

when this would be what i do in other languages
def foo(a as ParentClass, b as ParentClass)
if a inherits ChildClass and b inherits ChildClass
a = cast a as ChildClass
b = cast b as ChildClass
.doSomethingSpecial(a, b)

and this would be the ideal syntax
def foo(a as ParentClass, b as ParentClass)
if a inherits ChildClass and b inherits ChildClass
.doSomethingSpecial(a, b)


Is there a better way of doing it ?

Berseker59

Re: Type casting ?

PostPosted: Sun Apr 29, 2012 2:14 pm
by Charles
def foo(a as ParentClass, b as ParentClass)
if a inherits ChildClass and b inherits ChildClass
.doSomethingSpecial(a, b)

We almost have the above, but at this time it only works for simple expressions. Therefore, you have to stack them like so:

def foo(a as ParentClass, b as ParentClass)
if a inherits ChildClass
if b inherits ChildClass
.doSomethingSpecial(a, b)

That's called the "if-inherits statement". I do plan on making it support compound expressions at some point in the future.

The casting operating is "to" so you could also write this:
def foo(a as ParentClass, b as ParentClass)
if a inherits ChildClass and b inherits ChildClass
.doSomethingSpecial(a to ChildClass, b to ChildClass)

And if it applies to your situation, Cobra has overloading based on argument types (like C#, Java and others):
def foo(a as ParentClass, b as ParentClass)
print a, b

def foo(a as ChildClass, b as ChildClass)
print a, b

HTH.

Re: Type casting ?

PostPosted: Wed May 02, 2012 7:04 pm
by berseker59
Thanks a lot, that's all i wanted to know :D