默认类型 - 参数化函数文字类参数

时间:2010-03-23 20:02:15

标签: scala default-value type-parameter function-literal

这是预期的行为还是错误?考虑以下特征(无论是类,无所谓):

trait P[T] {
    class Inner(val f: T => Unit = _ => println("nope"))
}

这就是我的预期:

scala> val p = new P[Int] {
     |     val inner = new Inner
     | }
p: java.lang.Object with P[Int]{def inner: this.Inner} = $anon$1@12192a9

scala> p.inner.f(5)
nope

但是这个?

scala> val p = new P[Int] {
     |     val inner = new Inner() {
     |         println("some primary constructor code in here")
     |     }
     | }
<console>:6: error: type mismatch;
 found   : (T) => Unit
 required: (Int) => Unit
           val inner = new Inner() {
                           ^

1 个答案:

答案 0 :(得分:2)

这似乎是一个错误,尽管在嵌套类,抽象类型和默认参数之间存在相当模糊的交集。你可以在Scala bug tracker中提出一张票 - 我找不到描述这个的现有票。

以下是它对typer阶段的看法:

 ~: scala -nocompdaemon -Xprint:typer -e 'trait P[T] { class Inner(val f: T = null.asInstanceOf[T]) }; new P[Int] { new Inner(){} }'
!!!
discarding <script preamble>
(fragment of scalacmd162105603941759154.scala):1: error: type mismatch;
 found   : T
 required: Int
trait P[T] { class Inner(val f: T = null.asInstanceOf[T]) }; new P[Int] { new Inner(){} }
                                                                               ^
[[syntax trees at end of typer]]// Scala source: (virtual file)
package <empty> {
  final object Main extends java.lang.Object with ScalaObject {
    def this(): object Main = {
      Main.super.this();
      ()
    };
    def main(argv: Array[String]): Unit = {
      val args: Array[String] = argv;
      {
        final class $anon extends scala.AnyRef {
          def this(): anonymous class $anon = {
            $anon.super.this();
            ()
          };
          abstract trait P[T >: Nothing <: Any] extends java.lang.Object with ScalaObject {
            def /*P*/$init$(): Unit = {
              ()
            };
            class Inner extends java.lang.Object with ScalaObject {
              <paramaccessor> private[this] val f: T = _;
              <stable> <accessor> <paramaccessor> def f: T = Inner.this.f;
              def this(f: T = null.asInstanceOf[T]): P.this.Inner = {
                Inner.super.this();
                ()
              }
            };
            final <synthetic> object Inner extends java.lang.Object with ScalaObject {
              <synthetic> def init$default$1: T @scala.annotation.unchecked.uncheckedVariance = null.asInstanceOf[T];
              def this(): object P.this.Inner = {
                Inner.super.this();
                ()
              }
            }
          };
          {
            final class $anon extends java.lang.Object with this.P[Int] {
              def this(): anonymous class $anon = {
                $anon.super.this();
                ()
              };
              {
                final class $anon extends $anon.this.Inner {
                  def this(): anonymous class $anon = {
                    $anon.super.this(P.this.Inner.<error: method init$default$1>);
                    ()
                  };
                  <empty>
                };
                new $anon()
              }
            };
            new $anon()
          }
        };
        {
          new $anon();
          ()
        }
      }
    }
  }
}

工作版本,没有匿名内部类扩展内部。

 ~: scala -nocompdaemon -Xprint:typer -e 'trait P[T] { class Inner(val f: T = null.asInstanceOf[T]) }; new P[Int] { new Inner() }'
[[syntax trees at end of typer]]// Scala source: (virtual file)
package <empty> {
  final object Main extends java.lang.Object with ScalaObject {
    def this(): object Main = {
      Main.super.this();
      ()
    };
    def main(argv: Array[String]): Unit = {
      val args: Array[String] = argv;
      {
        final class $anon extends scala.AnyRef {
          def this(): anonymous class $anon = {
            $anon.super.this();
            ()
          };
          abstract trait P[T >: Nothing <: Any] extends java.lang.Object with ScalaObject {
            def /*P*/$init$(): Unit = {
              ()
            };
            class Inner extends java.lang.Object with ScalaObject {
              <paramaccessor> private[this] val f: T = _;
              <stable> <accessor> <paramaccessor> def f: T = Inner.this.f;
              def this(f: T = null.asInstanceOf[T]): P.this.Inner = {
                Inner.super.this();
                ()
              }
            };
            final <synthetic> object Inner extends java.lang.Object with ScalaObject {
              <synthetic> def init$default$1: T @scala.annotation.unchecked.uncheckedVariance = null.asInstanceOf[T];
              def this(): object P.this.Inner = {
                Inner.super.this();
                ()
              }
            }
          };
          {
            final class $anon extends java.lang.Object with this.P[Int] {
              def this(): anonymous class $anon = {
                $anon.super.this();
                ()
              };
              new $anon.this.Inner($anon.this.Inner.init$default$1)
            };
            new $anon()
          }
        };
        {
          new $anon();
          ()
        }
      }
    }
  }
}