[plt-scheme] The problem with Scala (OT, sorry)

From: Will M. Farr (wmfarr at gmail.com)
Date: Thu Mar 4 17:04:39 EST 2010

On Mar 4, 2010, at 3:41 PM, Raoul Duke wrote:

> On Thu, Mar 4, 2010 at 1:11 PM, John Clements <clements at brinckerhoff.org> wrote:
>> That is: the type checker couldn't figure out what kind of thing (List()) was, so it conservatively decided that it was List[Nothing].  Then, you get an error at some arbitrarily distant location. You can solve this by adding a type annotation to List(). Bflegg.
> 
> it is my rough anecdotal hear-say understanding that this happens in
> other languages that have type inference. iiuc, the wisdom for
> programming in ocaml is to actually put in type annotations anyway so
> that errors show up closer to the real human error, rather than miles
> away due to a type checker that manged to keep on trucking.

Actually, here's what OCaml does (I think I understand what's happening with the Scala code):

# let x = [];;
val x : 'a list = []
# List.fold_left (fun accu elt -> List.append accu elt) x [[1;2]; [3;4]];;
- : int list = [1; 2; 3; 4]
# ...

There are also funny '_a types in OCaml which means that the polymorphism is "used up", but we don't yet know what type to use for it:

# let y = ref [];;
val y : '_a list ref = {contents = []}
# List.fold_left (fun accu elt -> List.append accu elt) !y [[1; 2]; [3; 4]];;
- : int list = [1; 2; 3; 4]
# y;;
- : int list ref = {contents = []}
# ...

At the beginning, the "box" (OCaml calls them references) y stores a list of *some* definite type, but we can't tell what it is yet.  Once the fold_left has happened, then we know that the type of y must be int list ref.  

Will

Posted on the users mailing list.