Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save varmas/d3df4cf26f030e595c451a6dd57540dc to your computer and use it in GitHub Desktop.
Save varmas/d3df4cf26f030e595c451a6dd57540dc to your computer and use it in GitHub Desktop.
dynamic class instantiation in scala [using structural typing]
package a.b.c
class ABC {
def foo(bar: String): String = s"wololo.... $bar"
}
object ABCRunner {
type Sample = { def foo(bar: String): String }
def main(args: Array[String]) {
val baz = Class.forName("a.b.c.ABC").newInstance.asInstanceOf[Sample]
println(baz.foo("boo"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment