等同于特质的单一抽象方法?

时间:2019-05-06 22:14:02

标签: scala

考虑此特征:

trait Foo {
  def m1(id : Int) : Try[String]
}

还有一个实例:

  val g : Foo = new Foo {
    override def m1(id: Int): Try[String] = Success("Good job")
  }

Intellij建议将其转换为单一抽象方法:

  val g : Foo = (id: Int) => Success("Good job")

这两个等效吗?如何?

1 个答案:

答案 0 :(得分:0)

让我们检查scalac的作用:

import scala.util.{Success, Try}

trait Foo {
  def m1(id : Int) : Try[String]
}

object MainClass {
  val g : Foo = new Foo {
    override def m1(id: Int): Try[String] = Success("Good job")
  }
}

运行:

$ scalac -print src/main/scala/MainClass.scala

并输出:

[[syntax trees at end of                   cleanup]] // MainClass.scala
package <empty> {
  abstract trait Foo extends Object {
    def m1(id: Int): scala.util.Try
  };
  object MainClass extends Object {
    private[this] val g: Foo = _;
    <stable> <accessor> def g(): Foo = MainClass.this.g;
    def <init>(): MainClass.type = {
      MainClass.super.<init>();
      MainClass.this.g = {
        new <$anon: Foo>()
      };
      ()
    }
  };
  final class anon$1 extends Object with Foo {
    override def m1(id: Int): scala.util.Try = new scala.util.Success("Good job");
    def <init>(): <$anon: Foo> = {
      anon$1.super.<init>();
      ()
    }
  }
}

并对以下内容执行相同的步骤

import scala.util.{Success, Try}

trait Foo {
  def m1(id : Int) : Try[String]
}

object MainClass {

  def main(args: Array[String]): Unit = {
    val g : Foo = (id: Int) => Success("Good job")
  }
}

运行:

$ scalac -print src/main/scala/MainClass.scala

输出:

[[syntax trees at end of                   cleanup]] // MainClass.scala
package <empty> {
  abstract trait Foo extends Object {
    def m1(id: Int): scala.util.Try
  };
  object MainClass extends Object {
    private[this] val g: Foo = _;
    <stable> <accessor> def g(): Foo = MainClass.this.g;
    final <artifact> private[this] def $anonfun$g$1(id: Int): scala.util.Try = new scala.util.Success("Good job");
    def <init>(): MainClass.type = {
      MainClass.super.<init>();
      MainClass.this.g = {
        ((id: Int) => MainClass.this.$anonfun$g$1(id))
      };
      ()
    }
  }
}

如您所见,它们对于编译器而言是不同的。

在第一种情况下,它是一个匿名对象,在第二种情况下,它是一个匿名函数。