Created
August 22, 2011 09:41
-
-
Save jackywyz/1162029 to your computer and use it in GitHub Desktop.
scala self-types and Structure-type
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
abstract class Ta{ | |
def say(a:Int):Int =a+2 | |
} | |
trait T{ | |
def say(a:Int):Int = a | |
} | |
//有实体函数的trait T会编译成 | |
abstract trait T$class extends { | |
def say($this: T, a: Int): Int = a; | |
def /*T$class*/$init$($this: T): Unit = { | |
() | |
} | |
}; | |
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
object SelfTypes { | |
trait MustHave1 | |
trait MustHave2 | |
trait Independent | |
// constrain the traits A, B and C | |
trait A { self: MustHave1 => } | |
trait B { self: MustHave2 => } | |
trait C { self: MustHave1 with MustHave2 => } | |
// correct usage | |
class Test1 extends MustHave1 with A | |
class Test2 extends MustHave2 with B | |
class Test3 extends MustHave1 with MustHave2 with C | |
class Test4 extends MustHave1 with Independent with A | |
class Test5 extends Test1 with A | |
class Test6 extends Test3 with A with B | |
// -- these examples do not type check | |
// class Wrong1 extends A | |
// class Wrong2 extends A with Independent | |
// class Wrong3 extends MustHave2 with A | |
// class Wrong4 extends MustHave1 with C | |
// mixing the behaviours Resize and ColorCount in our domain Image | |
case class Image(width: Int, height: Int, buf: Array[Byte]) | |
trait Resize { self: Image => | |
def resize(newWidth: Int, newHeight: Int): Image = { | |
// a really stupid implementation | |
Image(newWidth, newHeight, buf.slice(0, newWidth * newHeight)) | |
} | |
} | |
trait ColorCount { self: Image => | |
def count(color: Byte) = buf.foldLeft(0) {(a, v) => if(v == color) a + 1 else a } | |
} | |
def main(args: Array[String]) = { | |
val i = new Image(5, 3, Array(1,2,2,2,2,1,3,3,3,3,1,2,2,2,2)) | |
with Resize | |
with ColorCount | |
val n = i.resize(3, 1) | |
assert(n.width == 3) | |
assert(n.height == 1) | |
assert(n.buf.size == (n.width * n.height)) | |
assert(i.count(1) == 3) | |
assert(i.count(2) == 8) | |
assert(i.count(3) == 4) | |
} | |
} |
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 Te{ | |
def call:String = "Hello" | |
} | |
object Te{ | |
def apply() = new Te() | |
def foo(callable: { def call: String }) = println(callable call) | |
def main(args:Array[String]){ | |
this foo this() | |
} | |
} | |
Author
jackywyz
commented
Oct 27, 2011
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment