"Proxies" with implicits and singleton types
| 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