#2447 Filling a list with nulls

mattgwwalker Thu 27 Aug 2015

Ok, so I'm completely a newbie with Fantom.

Having got that out of the way... I would like to fill a list (of type "Float?") with nulls. I felt this should work:

List.make( Float?#, 3 ).fill(null, 3)

But this gives me

Invalid args fill(sys::V, sys::Int), not (null, sys::Int)

I tried to explore this with the use of [,] and I am quite confused as to what the story might be:

// These two commands produce a list of the same type
fansh> [,].typeof
fansh> List.make(Obj?#, 3).typeof

// But only one of the two can be filled with nulls
fansh> [,].fill(null, 3)
[null, null, null]
fansh> List.make(Obj?#, 3).fill(null, 3)
ERROR(25): Invalid args fill(sys::V, sys::Int), not (null, sys::Int)

What am I doing wrong with the last command?

All assistance greatly appreciated!


SlimerDude Thu 27 Aug 2015

Hi Matthew,

Welcome to Fantom!

You may have stumbled on a bug there, for I too would have expected to be able to fill a list with nulls. Brian should be able to verify.

In the mean time, try this:

fansh> list := Float?[,]
fansh> list.size = 3
fansh> list
[null, null, null]

The first line creates an empty list of (nullable) Floats, and the second expands it to a size of 3. The List.size() docs say:

If the size is set greater than the current size then the list is automatically grown to be a sparse list with new items defaulting to null.

matthew Thu 27 Aug 2015

This should work

(Float?[,]).fill(null, 3)

brian Thu 27 Aug 2015

In general, you don't ever want to fill using null because arrays already default to null when explicitly resized. So best pattern is:

list := Float?[,]
list.size = 3

The automatically increases it size and makes everything up to size null. You can only increase size of a nullable list though

mattgwwalker Fri 28 Aug 2015

SlimerDude, thanks for the suggestion that it's a bug. I was pretty confused about this behaviour.

Brian, thanks for the confirmation of this pattern to fill with nulls by specifying the size.


SlimerDude Fri 28 Aug 2015

@matthew I've not come across this before, what's the difference between:

Float?[,] .fill(null, 3) and
(Float?[,]).fill(null, 3) ???

And why does one syntax allow the list to be filled with null's and other does not!?

Jeremy Criquet Fri 28 Aug 2015

As far as I'm aware, they're the same. I'm pretty sure a Type that is before an empty list ( [,] ) is parsed before a dot operator after it.

Login or Signup to reply.