Skip to content

Instantly share code, notes, and snippets.

@fancellu
Last active November 15, 2019 08:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fancellu/713602e68746336b1dc41c9dab85d4a5 to your computer and use it in GitHub Desktop.
Save fancellu/713602e68746336b1dc41c9dab85d4a5 to your computer and use it in GitHub Desktop.
Some nice tricks with single abstract method interface, Scala 2.11+
object SingleAbstractMethodSyntax extends App{
// Some nice tricks with single abstract method interface, Scala 2.11+
trait DoIt{
def doSomething(x:Int): Int
}
// I am creating an instance of DoIt, and supplying doSomething
val doit: DoIt= _+1
println(doit.doSomething(10))
trait DoIt2{
def concrete=123
def apply(x:Int): Int
}
// I am creating an instance of DoI2t, and supplying apply. Note we only have 1 undefined member, apply
val doit2: DoIt2= _+2
println(doit2(10))
println(doit2.concrete)
// Note, Runnable is also a single method interface, def run(): Unit, so we can say
val newthread=new Thread(()=> println(s"Hello from ${Thread.currentThread}"))
newthread.setName("newthread")
newthread.start()
newthread.join()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment