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

From: John Clements (clements at brinckerhoff.org)
Date: Thu Mar 4 17:52:03 EST 2010

On Mar 4, 2010, at 2:37 PM, Prabhakar Ragde wrote:

> John Clements wrote:
> 
>> It's possible (probable?) that type systems for (e.g.) Haskell and
>> OCaml have funny corner cases, and they can certainly also produce
>> bad error messages, but to the best of my knowledge they don't ever
>> "guess" a type and then keep going. So, for instance, I believe it's
>> the case at least for the core of the language that deleting all of
>> the type annotations should not affect the semantics of the program.
>> Then again, Java's types are all tangled up with its semantics, so I
>> suppose I shouldn't be surprised. In this case, though, it would have
>> seemed a better choice to say "please give me a type for this empty
>> list" than to guess at the List[Nothing] type.
> 
> 
> The Odersky et al. book "Programming In Scala" has a very similar example in section 16.9, and explains that this is due to Scala's "local, flow-based typing algorithm" as opposed to the more general Hindley-Milner-style algorithms. It does not, however, give an example to demonstrate that the choice of "Nothing" is the right choice in some situations. --PR

Right.

Actually, their "An Overview of Scala" has this to say:

"Even though type Nothing is empty, it is nevertheless useful as a type parameter. For instance, the Scala library de􏰀nes a value Nil of type List[Nothing]. Because lists are covariant in Scala, this makes Nil an instance of List[T], for any element type T."

So, I now see that the "Nothing" choice actually *does* make sense, it's just that foldLeft can't infer the right list type for its argument.  Probably this is just a left-to-right problem, where a method that's parameterized over a type that occurs in two of its arguments takes the type appearing in the first one as the one to plug into the type parameter.

I don't think I'd have any trouble at all explaining that to a first-year student.

John

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2484 bytes
Desc: not available
URL: <http://lists.racket-lang.org/users/archive/attachments/20100304/44db6648/attachment.p7s>

Posted on the users mailing list.