This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
下のコードの「def id = i」の部分がコンパイルエラーになる。 | |
error: not found: value i | |
セルフアノテーションを外すと通るようになる。 | |
またはimportの前にidメソッドを定義するようにすると、通るようになる。 | |
*/ | |
trait T | |
class C(i: Int) {self: T => | |
private object Private {} | |
import Private._ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Scala Exsample Chapter 3 | |
http://www29.atwiki.jp/tmiya/pages/26.html | |
Scala2.8で、オークション終了後、シャットダウンするまでの間にOfferメッセージを | |
受け取ったときの問題を修正したもの。 | |
*/ | |
class Auction(seller: Actor, minBid: Int, closing: Date) extends Actor { | |
val timeToShutdown = 36000000 // msec | |
val bidIncrement = 10 | |
def act() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// このようなクラスがあったとき | |
class T(prev: => T, next: => T) | |
// それぞれに変数を用意する場合は、このようにすれば循環参照を作成できるが | |
lazy val a: T = new T(c, b) | |
lazy val b: T = new T(a, c) | |
lazy val c: T = new T(b, a) | |
// このようなかんじに不定個の要素で循環参照を作成したい場合はどうすればよいだろうか | |
def f(n: Int) = (1 to n).foldLeft(null:T)((ref, _) => new T(ref, null)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
implicit parameterを参照するには、コンパニオンオブジェクト内の定義を探す場合も、その定義位置より後でないとダメみたい。 | |
以下のコードだと | |
error: could not find implicit value for evidence parameter of type RichGraphics.Drawer[MyLine] | |
g.draw(MyLine(10, 30, 50, 20)) | |
というエラーになる。 | |
new Frame { ... } の部分をコードの一番最後に移動すると、 | |
うまく動くようになる。 | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 記号と文字列では記号のほうが優先順位が高い | |
case class A(v: Int) { | |
def *(a: A) = A(v * a.v) | |
def add(a: A) = A(v + a.v) | |
} | |
A(2) * A(3) add A(5) // A(11) | |
A(2) add A(5) * A(3) // A(17) | |
// 記号でも'='が付く場合は優先順位が最後になる | |
case class A(var v: Int) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// コンパニオンオブジェクトにあるimplicit parameterの探索には優先順位はない? | |
abstract class M[T] { def s: String } | |
object M { implicit object MA extends M[A] { def s = "M" } } | |
class A | |
object A { implicit object MA extends M[A] { def s = "A" } } | |
def f[T: M](t: T) = implicitly[M[T]].s | |
f(new A) // error: ambiguous implicit values |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// implicit parameter の探索 | |
class SM[T] | |
object SM { implicit object MA2 extends M[A2] } | |
class M[T] extends SM[T] | |
object M { implicit object MA extends M[A] } | |
class A | |
class A2 | |
class B | |
object B { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ビルドエラーになる | |
// 参考: http://d.hatena.ne.jp/rf0444/20100702/1277963898 | |
def boolToProc[C, A <% C, B <% C](b: Boolean): (=>A) => (=>B) => C = { | |
def t[X, Y](x: =>X)(y: =>Y) = x | |
def t[X, Y](x: =>X)(y: =>Y) = y | |
if (b) t _ else f _ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
context boundの使用例。 | |
SwingのGraphics2Dを拡張する。 | |
Graphics2DはStringやShape、Imageなど既存の対象を描画するメソッドがあるが | |
独自のオブジェクトも同じようなメソッドで描画できるよう、implicit conversionで拡張する。 | |
このとき、変換先のオブジェクトに直接、独自オブジェクトを描画するためのメソッドを定義するのではなく、 | |
描画処理を別のオブジェクトにしておくことで、後から独自オブジェクトの種類を追加したときにも | |
対応できるようになる。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A | |
class B extends A | |
class C | |
class D | |
implicit def d2a(d: D) = new A | |
val (a, b, c, d) = (new A, new B, new C, new D) | |
//def u[T <: A](x: T): A = x | |
def u[T](x: T)(implicit f: T <:< A): A = x |