Page 1 of 1

arrays of nilable single; Array.copy method

PostPosted: Mon Feb 18, 2013 1:53 pm
by DelphiGuy
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.

Re: arrays of nilable single; Array.copy method

PostPosted: Mon Feb 18, 2013 5:28 pm
by nerdzero
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

Re: arrays of nilable single; Array.copy method

PostPosted: Mon Feb 18, 2013 8:48 pm
by DelphiGuy
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!

Re: arrays of nilable single; Array.copy method

PostPosted: Tue Feb 19, 2013 3:51 am
by hopscc
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
...

Re: arrays of nilable single; Array.copy method

PostPosted: Wed Feb 20, 2013 11:26 am
by DelphiGuy
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?

Re: arrays of nilable single; Array.copy method

PostPosted: Mon Feb 25, 2013 3:51 pm
by nerdzero
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.