-
-
Save paulp/d911b4abad72ba2dc540 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
package proxy | |
import language.dynamics | |
abstract class Param[T] { type Type } | |
object Param { | |
def apply[T](name: String) = new Param[name.type] { type Type = T } | |
} | |
class Proxy(underlying: Map[String, Any]) extends Dynamic { | |
def selectDynamic[T](name: String)(implicit param: Param[name.type]): param.Type = | |
underlying(name).asInstanceOf[param.Type] | |
} | |
object Test { | |
val proxy = new Proxy(Map("foo" -> "Hello world", "bar" -> 42, "baz" -> 'xyz)) | |
implicit def foo = Param[String]("foo") | |
implicit def bar = Param[Int]("bar") | |
implicit def baz = Param[Symbol]("baz") | |
println(proxy.foo) | |
println(proxy.bar) | |
println(proxy.baz) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment