#2477 Nullable Map Literals

SlimerDude Tue 29 Sep 2015

Hi, I'm nit-picking here and I've largely written this for documentation reasons, but the following gives a compilation Err:

map := [Int:Int]?[:]
// --> Invalid map type '[sys::Int:sys::Int]?' for map literal

I tried creating one at runtime but got an ArgErr:

map := Map(Map#.parameterize(["K":Int#, "V":Int?#]).toNullable)
// --> sys::ArgErr: Non-nullable map type required: [sys::Int:sys::Int?]?

This patch fixes the ArgErr:

diff -r fe538d44fa4d src/sys/java/fan/sys/Map.java
--- a/src/sys/java/fan/sys/Map.java	Mon Sep 28 13:18:07 2015 -0400
+++ b/src/sys/java/fan/sys/Map.java	Tue Sep 29 10:26:24 2015 +0100
@@ -32,6 +32,11 @@
 
   public static Map make(Type type)
   {
+    if (type instanceof NullableType)
+    {
+      type = ((NullableType) type).root;
+    }
+
     MapType t = null;
     try
     {

But led me to the interesting observation that MapType, and hence Maps in general, can't truly be nullable:

fansh> m2 := Map(Map#.parameterize(["K":Int#, "V":Int#]).toNullable)
[:]
fansh> m2.typeof
[sys::Int:sys::Int]  // --> expected [sys::Int:sys::Int]?

Not that it's an issue - as I can't see why that signature would ever be needed.

brian Tue 29 Sep 2015

But led me to the interesting observation that MapType, and hence Maps in general, can't truly be nullable:

Subtle differences, but you need to separate the parameterized type of a Map instance versus a type signature - they are two separate things. For example this is allowed:

[Int:Int]? someMethod() { return null }

The method returns a map types Int:Int or null. However a given map instance by definition cannot be nullable. So the map type associated with it is non-nullable by definition.

SlimerDude Tue 29 Sep 2015

Okay, that makes sense - thanks for the explanation!

Login or Signup to reply.