#2777 Nullable Generic Lists

SlimerDude Thu 14 Nov

Generic lists do not appear to support null values - is that right?

I would have thought Obj?# would have been the catch all generic Type, so generic lists could be used in all situations.

fansh> list := List(Obj?#, 3)
fansh> nul  := null
fansh> list.add(nul)
sys::NullErr: Coerce to non-null
  fan.sys.NullErr.makeCoerce (NullErr.java:38)

fansh> list.add(null)
ERROR(10): Invalid args add(sys::V), not (null)

In a similar way:

fansh> list2 := (List) [,]
fansh> nul   := null
fansh> list2.add(nul)
sys::NullErr: Coerce to non-null
  fan.sys.NullErr.makeCoerce (NullErr.java:38)

fansh> list.add(null)
ERROR(10): Invalid args add(sys::V), not (null)

brian Thu 14 Nov

No that is not really accurate. The issue is that without a specific cast or parameterized generic, then V (or any generic parameter) will be assumed to be Obj (not Obj?). So the explicit List.make constructor will essentially default to List<Obj>. You can work around it like this:

list := (Obj?[]) List.make(Obj?#, 0)

As a general principle I think its always best to avoid using List and Map types directly as opposed of their parameterized syntax

Login or Signup to reply.