How did anonymous function implements the trait?

前端 未结 1 1194
小鲜肉
小鲜肉 2021-01-16 11:33

let\'s see the code in scala REPL: first, i defined a trait:

trait Service{
  def invoke(name:String):String
}

then i defined an anonymous

1条回答
  •  心在旅途
    2021-01-16 12:01

    This is a new feature describe in the release notes of 2.12: http://www.scala-lang.org/news/2.12.0#lambda-syntax-for-sam-types

    The Scala 2.12 type checker accepts a function literal as a valid expression for any Single Abstract Method (SAM) type, in addition to the FunctionN types from standard library. This improves the experience of using libraries written for Java 8 from Scala code. Here is a REPL example using java.lang.Runnable:

    scala> val r: Runnable = () => println("Run!")
    r: Runnable = $$Lambda$1073/754978432@7cf283e1
    scala> r.run()
    Run!
    

    Note that only lambda expressions are converted to SAM type instances, not arbitrary expressions of FunctionN type:

    scala> val f = () => println("Faster!")
    scala> val fasterRunnable: Runnable = f
    :12: error: type mismatch;
    found   : () => Unit
    required: Runnable
    

    The language specification has the full list of requirements for SAM conversion.

    With the use of default methods, Scala’s built-in FunctionN traits are compiled to SAM interfaces. This allows creating Scala functions from Java using Java’s own lambda syntax:

    public class A {
      scala.Function1 f = s -> s.trim();
    }
    

    Specialized function classes are also SAM interfaces and can be found in the package scala.runtime.java8.

    Thanks to an improvement in type checking, the parameter type in a lambda expression can be omitted even when the invoked method is overloaded. See #5307 for details. In the following example, the compiler infers parameter type Int for the lambda:

    scala> trait MyFun { def apply(x: Int): String }
    scala> object T {
        |   def m(f: Int => String) = 0
        |   def m(f: MyFun) = 1
        | }
    scala> T.m(x => x.toString)
    res0: Int = 0
    

    Note that though both methods are applicable, overloading resolution selects the one with the Function1 argument type, as explained in more detail below.

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