#1874 Map type question

paul Sun 15 Apr 2012

Just to make things clear for myself. Here's an example

class HelloFantom {

  Void main() {
    Map data := ["x": null]
    z := data.get("x") ?: 2
    echo(z)
  }
}

This code doesn't compile throwing:

Compiler Error:
in line (5) , col(15): Cannot use '?:' operator on non-nullable type 'sys::Obj'

It seems that the compiler fails to detect the right type of map data which is [Str:Obj?]. Thus if you try to compile and run this code:

class HelloFantom {

  Void main() {
    Map data := ["x": null]
    echo(data.typeof)
  }
}

The output will be

[sys::Str:sys::Obj?]

Is this a bug?

brian Sun 15 Apr 2012

In your example this line:

Map data := ["x": null]

The type of the variable is explicitly typed as Map, not Str:Obj?. The actual map it references is typed Str:Obj?, but not the variable itself. If you used type inference, then map would have the type you expect:

data := ["x": null]

Although there is still a sort of bug there in that an unparameterized Map should still return V? for get. There is still a few rough edges using unparameterized generic types like that.

Login or Signup to reply.