Created
August 18, 2009 13:33
-
-
Save NicolasT/169716 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// This type is defined in some other library we can't modify | |
class SomeObject(val a: Int, val b: String) { | |
def c = "a = " + a + ", b = " + b | |
} | |
// This is our code | |
object DynamicDemo { | |
// A function which takes anything which has a member 'a' of type Int and a | |
// member 'd' of type String | |
def f(arg: {def a: Int; def d: String}) = "f says: " + arg.a + " " + arg.d | |
// Testing function | |
def main(args: Array[String]): Unit = { | |
println("Test 1") | |
// We can create a | |
val r1 = f(new {val a = 123; val d = "456"}) | |
println(r1) | |
println("Test 2") | |
val o = new SomeObject(789, "987") | |
// Notice we can just pass a SomeObject instance to f! | |
val r2 = f(o) | |
println(r2) | |
} | |
implicit def someObjectToFArg(o: SomeObject): | |
{def a: Int; def d: String} = new {val a = o.a; val d = o.c} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment