Forums

Type casting ?

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

Type casting ?

Postby berseker59 » Sun Apr 29, 2012 10:28 am

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
berseker59
 
Posts: 2

Re: Type casting ?

Postby Charles » Sun Apr 29, 2012 2:14 pm

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

Re: Type casting ?

Postby berseker59 » Wed May 02, 2012 7:04 pm

Thanks a lot, that's all i wanted to know :D
berseker59
 
Posts: 2


Return to Discussion

Who is online

Users browsing this forum: No registered users and 9 guests