#2217 Wot no type inference from List.map?

SlimerDude Sat 7 Dec 2013

This is just a comment really, rather than a question...

I was surprised that there's no type inference when using List.map, for the following gives a compilation Err.

class Example {
  Void main() {
    types := #main.params.map |param->Type| { param.type }

    types.first.qname	// Err --> Unknown slot 'sys::Obj.qname'
  }
}

brian Mon 9 Dec 2013

The generics system is not capable of typing that expression at compile type. So map always returns a generic list at compile time. However, the runtime does look at the type of the mapping function to allocate the correct list type. Consider:

Type[] types := #main.params.map |param->Type| { param.type }
echo(types.typeof)
echo(types.first.qname)

The runtime type of types is Type[] which actually means on the JVM that the backing store is a Type[] array.

Login or Signup to reply.