Scala single method interface implementation

前端 未结 3 652
夕颜
夕颜 2020-12-03 20:45

Does Scala have any syntactic sugar to replace the following code:

val thread = new Thread(new Runnable {
   def run() {
     println(\"hello world\")
   }           


        
相关标签:
3条回答
  • 2020-12-03 21:32

    While doing this in a generic way is certainly complicated, if you found that you really only needed this for a few certain Java types, then a few simple implicit conversions can do the job nicely. For instance:

    val thread = new Thread(() => println("hello world"))
    thread.start
    
    implicit def function0ToRunnable(f:() => Unit):Runnable = 
      new Runnable{def run() = f()}
    

    Sometimes trying to solve a problem in a generic and completely re-useable way is the wrong approach if your actual problem is more bounded then you think.

    0 讨论(0)
  • 2020-12-03 21:37

    SAM types are supported using invokeDynamic since scala-2.12 similar to JDK-8, Below was tested on 2.12.3 - Release notes about SAM can be found here - http://www.scala-lang.org/news/2.12.0/

    object ThreadApp extends App {
      println("Main thread - begins")
      val runnable: Runnable = () => println("hello world - from first thread")
    
      val thread = new Thread(runnable)
      println("Main thread - spins first thread")
      thread.start()
    
      val thread2 = new Thread(() => println("hello world - from second thread"))
      println("Main thread - spins second thread")
      thread2.start
      thread.join()
      thread2.join()
    
      println("Main thread - end")
    }
    
    0 讨论(0)
  • 2020-12-03 21:43

    Scala has experimental support for SAMs starting with 2.11, under the flag -Xexperimental:

    Welcome to Scala version 2.11.0-RC3 (OpenJDK 64-Bit Server VM, Java 1.7.0_51).
    Type in expressions to have them evaluated.
    Type :help for more information.
    
    scala> :set -Xexperimental
    
    scala> val r: Runnable = () => println("hello world")
    r: Runnable = $anonfun$1@7861ff33
    
    scala> new Thread(r).run
    hello world
    

    Edit: Since 2.11.5, this can also be done inline:

    scala> new Thread(() => println("hello world")).run
    hello world
    

    The usual limitations about the expected type also apply:

    • it must define a single abstract method,
    • its primary constructor (if any) must be public, no-args, not overloaded,
    • the abstract method must take a single argument list,
    • the abstract method must be monomorphic.

    According to the original commit by Adriaan, some of those restrictions may be lifted in the future, especially the last two.

    0 讨论(0)
提交回复
热议问题