Skip to content

Instantly share code, notes, and snippets.

@paulp
Forked from propensive/proxies.scala
Last active August 29, 2015 14:14
Show Gist options
  • Save paulp/d911b4abad72ba2dc540 to your computer and use it in GitHub Desktop.
Save paulp/d911b4abad72ba2dc540 to your computer and use it in GitHub Desktop.
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