Scala難しい

Scalaの型推論について - Togetterを試してみた

Welcome to Scala version 2.9.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_05).

scala> def id1[T](x : T) = x;
id1: [T](x: T)T

scala> def id2[T] = (x : T) => x;
id2: [T]=> T => T

scala> id1(id1)
<console>:9: error: missing arguments for method id1 in object $iw;
follow this method with `_' if you want to treat it as a partially applied function
              id1(id1)
                  ^

scala> id1(id2)
res1: Nothing => Nothing = <function1>

scala> id2(id2)
res3: Nothing => Nothing = <function1>

scala> id2(id2)(1)
<console>:9: error: type mismatch;
 found   : Int(1)
 required: Nothing
              id2(id2)(1)
                       ^

id1を引数に渡せないのはまあわかるけど、間違えたので記録に残してく

GHCi, version 6.10.4: http://www.haskell.org/ghc/  :? for help
Prelude> let id1 = \x -> x
Prelude> let foo = id1 id1
Prelude> :t foo
foo :: t -> t

まあ「型推論と一言に言っても言語によって推論規則とか違うよ」という例として出したかっただけだからこれでいいか。