#2258 Static type inference over List.map

tomcl Fri 11 Apr 2014

idlist := [1,2]
Project [] prlist := idlist.map |Int n->Project|{Project{ id = n}}
echo("${prlist[0].typeof}")
echo("prlist:${prlist[0].id}")

This code works as expected, but if the static type for prlist is not specified, but inferred, then prlist becomes static Obj [] and this code fails static type checking, even though it will work if the id field is accessed dynamically:

idlist := [1,2]
prlist := idlist.map |Int n->Project|{Project{ id = n}}
echo("${prlist[0].typeof}")
echo("prlist:${prlist[0]->id}")

I thought (naively perhaps) that static type inference on List and Func would be precise enough to resolve the static type here:

idlist: Int []

map function: Int->Project

should mean that:

idlist.map is inferred as static type Project

From the documentation the signature of map is:

Obj?[] map(|V,Int->Obj?| c)

Would it not be consistent to add limited generics (AKA polymorphic typing) as a special case to map and reduce so they work properly?

map: R[] map(|V,Int->R| c)

reduce: R reduce(R init, |R,V,Int->R| c)

I'm sure this has been worked through here, but I can't find it. Mending map and reduce would not be as complex as allowing user-defined generics.

Tom

brian Fri 11 Apr 2014

The runtime type is inferred correctly from the map function's return type. However the compile-time type is not. To do that properly requires a much more sophisticated type system which Fantom does not have.

tomcl Fri 11 Apr 2014

I was hoping that having map and reduce generically typed would be no worse (in terms of type system complexity) than having get, add, addall generically typed which happens now.

Perhaps this is not true?

Login or Signup to reply.