Skip to content

Instantly share code, notes, and snippets.

@xuwei-k
Created October 22, 2011 18:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xuwei-k/1306328 to your computer and use it in GitHub Desktop.
Save xuwei-k/1306328 to your computer and use it in GitHub Desktop.
Scala dependent method types ?

Scala dependent method types ?

ついに、ねんがんのでぃぺんでんとめそっどたいぷをてにいれたぞ

新しく 2.10 から(?) dependent method types っていう機能が Scala に入るらしいので、最新版をゴニョゴニョしてみた。 2.9.1で同じことやろうとすると def withFoo(foo: Foo): foo.Bar = foo.f という部分がコンパイル通らないはず。fooという 引数 の抽象型であるBarに依存した型だからっていう感じ?

しかし、機能の概念はなんとなくぼんやりと、把握はできたが、まだどういったときに役に立つのかわかっていない(・ω・`)

参考URLのメモ

resolvers += ScalaToolsSnapshots
scalaVersion := "2.10.0-SNAPSHOT"
Welcome to Scala version 2.10.0.r25873-b20111022020208 (OpenJDK 64-Bit Server VM, Java 1.6.0_20).
Type in expressions to have them evaluated.
Type :help for more information.
scala> trait Foo { type Bar ; val f:Bar }
defined trait Foo
scala> object Foo1 extends Foo{type Bar = Int; val f = 1}
defined module Foo1
scala> object Foo2 extends Foo{type Bar = String ; val f = "a"}
defined module Foo2
scala> def withFoo(foo: Foo): foo.Bar = foo.f
withFoo: (foo: Foo)foo.Bar
scala> withFoo(Foo1)
res0: Foo1.Bar = 1
scala> withFoo(Foo2)
res1: Foo2.Bar = a
scala> res0.getClass
res2: Class[Foo1.Bar] = int
scala> res1.getClass
res3: Class[_ <: Foo2.Bar] = class java.lang.String
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment