Last active
November 15, 2019 08:55
-
-
Save fancellu/713602e68746336b1dc41c9dab85d4a5 to your computer and use it in GitHub Desktop.
Some nice tricks with single abstract method interface, Scala 2.11+
This file contains 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
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