Forums

Array to List conversion

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

Array to List conversion

Postby _Gold_Fish » Fri Jul 09, 2010 2:55 pm

I've noticed that the way to convert an object from an Array to a List is via the .toList method. It seems to me that it would make more sense if it was done via to "to" keyword, so that instead of:
list = foo.toList

it would be
list = foo to List

or
list = foo to List<of *>

Is there any particular reason that this is not the case?
_Gold_Fish
 
Posts: 18

Re: Array to List conversion

Postby hopscc » Sun Jul 11, 2010 12:13 am

I'll take a stab.

Probably cos 'to' was chosen in cobra to act as the 'casting' operator ( from VB ?) to retype an object to something else up or down its inheritance or implementation hierarchy.

Personally I wouldnt say its advantageous to provide a language construct/operator to provide a mapping to a public library class conversion method unless you want to mask the Library access in some way especially given that language evolution has tended to be in the opposite direction (toward providing such capability via libraries directly rather than language builtins/RunTime Support systems)
and the range of possible conversions is arbitrarily large while the range of valid conversions is bounded.
Code: Select all
# Invented examples and syntax...

a = foo to Window
# Cannot convert foo which is Type Array (of ?) to Type Window
a = foo to CheeseBurgaz
# Cannot convert foo which is Type Array (of ?) to Type CheeseBurgaz

a = foo to List


Note that you can get something close to your conversion (array to List) using a cast from an array to an IList (interface) since an array implements IList (directly or indirectly)
Code: Select all
an = @[10,20,30]   # array
al = an to Collections.IList
for item in al
   print item
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Array to List conversion

Postby Charles » Sun Jul 11, 2010 7:59 pm

Hops is correct. In general, "obj.toFoo" is making a new Foo object from the contents of "obj". Another example is .NET's own "obj.toString".

"obj to Type" is a typecast which generally does not change "obj" and will throw an exception if "obj" is not already compatible with "Type" at run-time. There is a "to?" operator if you would prefer nil instead of such an exception.

But a notable exception to the above rules are numeric casts like "someInt to decimal" and "someInt32 to int64" which do in fact produce new values. This was basically inherited from .NET and other languages although I wonder why it shouldn't have been "someInt.toDecimal" and "someInt32.toInt64". Under the hood of the VM, I think a numeric cast must be radically different than the other casts.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Array to List conversion

Postby hopscc » Sun Jul 11, 2010 11:19 pm

Yeah - It falls back to casting just like pre OO languages... 1/2 :)
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Array to List conversion

Postby _Gold_Fish » Mon Jul 12, 2010 6:31 am

Ok, thanks for the clear-up!
_Gold_Fish
 
Posts: 18


Return to Discussion

Who is online

Users browsing this forum: No registered users and 64 guests