New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Type error when instantiating an object with a default argument #9970
Comments
Imported From: https://issues.scala-lang.org/browse/SI-9970?orig=1 |
@som-snytt said: scala> class A[T] { def f: T => Map[Int, Int] = _ => Map.empty } // print
class A[T] {^J def f: _root_.scala.Function1[T, scala.Predef.Map[scala.Int, scala.Int]] = ((x$1: T) => scala.Predef.Map.empty[Int, Nothing])^J} // : <notype> Unfortunately, // print completion doesn't work for the example. |
@SethTisue said: |
Diogo Sousa (orium) said: In fact, as @apm said, it didn't infer To be more general: I don't think it is acceptable to get a type error when a default parameter is used. It was typechecked before, it must fail at definition of the class/method if the types don't checkout (Another way this is weird is that |
@som-snytt said: class C[A] extends scala.AnyRef {
<paramaccessor> private[this] val f: A => Map[Int,Int] = _;
def <init>(f: A => Map[Int,Int] = ((x$1: A) => scala.Predef.Map.empty[Int, Nothing])): C[A] = {
C.super.<init>();
()
}
};
<synthetic> object C extends AnyRef {
def <init>(): C.type = {
C.super.<init>();
()
};
<synthetic> def <init>$default$1[A]: A => scala.collection.immutable.Map[Nothing,Nothing] = ((x$1: A) => scala.Predef.Map.empty[Nothing, Nothing])
}
} I think there's an open ticket for default args, of course I can't find it. Maybe similar to #8884. Spec behavior. |
@lrytz said: There's a special case with default arguments for parametric types, to support the following: scala> def f[T](x: T = 1) = x
f: [T](x: T)T
scala> f()
res0: Int = 1
scala> f[String]()
<console>:13: error: type mismatch;
found : Int
required: String
Error occurred in an application involving default arguments.
f[String]()
^ For the original example in your report, I think it's a valid bug. |
could you pls confirm that the following behavior is related to this issue
|
The compiler allows to define this
which implies that it infered that
Map.empty
to be of typeMap[Int, Int]
, if we instanciatenew A[String]()
the compilation fails withThe text was updated successfully, but these errors were encountered: