Forums

arrays of nilable single; Array.copy method

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

arrays of nilable single; Array.copy method

Postby DelphiGuy » Mon Feb 18, 2013 1:53 pm

1)
Code: Select all
def arrayDivision( numerator as float32[]?, denominator as float32[]?) as float32[]?
      assert numerator.length == denominator.length
      quotient = float32[](numerator.length) to ?  #proper cobra instantiation of array of nilable singles?
      for i in numerator.length
         if numerator[i] == nil or denominator[i] == nil
            quotient[i] = nil
         else
            quotient[i] = numerator[i] / denominator[i]
      return quotient


on the two lines of code above that include the word "nil", the compiler balks and complains that i'm trying to assign (or evaluate) nil to a float32. i thought, but obviously am mistaken, that my numerator, denominator and quotient were all declared as arrays of type nilable float32?. where did i go wrong, please?

2)
Code: Select all
def doSomething( today as float32[]? ) as float32[]?
      yesterday = float32[](today.length) to ?
      Array.copy(today to Array, 1, yesterday to Array, 2, today.length - 2)
      return yesterday


in pursuit of the ".copy" method, why must i cast type today and yesterday to type "Array"? why doesn't array of "float32" have its own .copy method? what is type "Array"? in short, can someone please point me to a link that explains the difference between type "Array" and one of the specific array types like float32[] ? i can't find a discussion of the difference, because no doubt i'm looking for the wrong words or concepts, or perhaps I'm not properly grasping an inheritance issue. thanks.
DelphiGuy
 
Posts: 116

Re: arrays of nilable single; Array.copy method

Postby nerdzero » Mon Feb 18, 2013 5:28 pm

1)

I think what you want is
tenNilableFloats = float32?[](10)

since then the elements of the array are nilable as opposed to the array itself being nilable.
nilableFloatArray as float32[]? = nil


But, I think there's some bugs in the compiler with this syntax because explicit typing doesnt' seem to work which prevents that from being used as a parameter type

def foo(nilableFloats as float32?[])
pass

That gives me:

Code: Select all
error: Expecting COMMA, but got "[" (LBRACKET) instead.


changing the parameter type to IList<of float32?> doesn't compile due to a supposed type mismatch but using a stream like this:
def foo(nilableFloats as float32?*)
pass

seems to work okay. However, since you are checking that the lengths of the arrays are equal I'm not sure they will help you in this case. I would open a ticket if I were you and use List<of float32?> instead of arrays in the meantime if you can.

2)

Array.copy is a shared/static method. I believe you want to use the .copyTo method instead like
someArray.copyTo(someOtherArray, 10)
See here: http://msdn.microsoft.com/en-us/library ... array.aspx
nerdzero
 
Posts: 286
Location: Chicago, IL

Re: arrays of nilable single; Array.copy method

Postby DelphiGuy » Mon Feb 18, 2013 8:48 pm

1) nerdzero said:
[you want to make] the elements of the array ... nilable as opposed to the array itself being nilable.


yes, i had the same thought myself, that the syntax i was using sure looked like i was making the array nilable, instead of the elements of the array nilable. and i wanted the latter, but since the compiler refused to cooperate i assumed it was my mistake.

thanks for the workaround. i'll open a ticket.

2) ah. i had that MS page on arrays already. indeed that's where i got the .copy syntax from. i'll try the .copyTo as you suggested. thanks!
DelphiGuy
 
Posts: 116

Re: arrays of nilable single; Array.copy method

Postby hopscc » Tue Feb 19, 2013 3:51 am

There is indeed a parse bug for parsing the Type decl of Arrays with nilable elements
- silently fails on a var type decl (dropping the arrayType :cry: ),
throws a parse failure for same on method calls which is what you caught (good stuff :) )
- on expressions (initializer) similar expression works fine :| .

I put a test program on the ticket (tkt-317) and a patch for a fix...

....
Onward
...
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: arrays of nilable single; Array.copy method

Postby DelphiGuy » Wed Feb 20, 2013 11:26 am

Is there a way to cast an array of non-nilable elements to an array whose elements are nilable?
Is there a way to cast a list of non-nilable elements to a list whose elements are nilable?
DelphiGuy
 
Posts: 116

Re: arrays of nilable single; Array.copy method

Postby nerdzero » Mon Feb 25, 2013 3:51 pm

I think this is something that can be done with an extension method. Since the ones from Linq aren't available I tried to extend IList<of T> but couldn't quite figure it out.

extend IList<of T>
def getAsNilable as IEnumerable<of T?>
for i in this
yield i to ?

class NotSureIfThisHowToDoIt

def main
nfs as IList<of float32?> = List<of float32?>()
nfs.add(nil)
nfs.add(nil)

.printFloats(nfs)

fs as IList<of float32> = List<of float32>()
fs.add(1.0f32)
fs.add(2.0f32)

.printFloats(fs.getAsNilable) # says type mismatch

def printFloats(floats as IEnumerable<of float32?>)
for f in floats
print f


As for arrays, maybe Array.convertAll would work? http://msdn.microsoft.com/en-us/library/exc45z53.aspx Can't say I've ever tried it though.
nerdzero
 
Posts: 286
Location: Chicago, IL


Return to Discussion

Who is online

Users browsing this forum: No registered users and 23 guests

cron