Skip to content

Instantly share code, notes, and snippets.

@NicolasT
Created August 18, 2009 13:33
Show Gist options
  • Save NicolasT/169716 to your computer and use it in GitHub Desktop.
Save NicolasT/169716 to your computer and use it in GitHub Desktop.
// 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