- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Ambiguous Constructors
Thu, 2009-01-22, 15:59
I found this snipet very confusing:
class Ambiguous(value: =>Unit){
def this() = this(println("invoking default constructor"))
def execute() = value
}
val amb = new Ambiguous{
println("invoking anonymous class constructor")
}
amb.execute
Output:
invoking anonymous class constructor
invoking default constructor
I was not specting an anonymous subclassing. I think the compiler
should raise a warning or an error to avoid these confusions.
- Andrés
Andrés Testi schrieb:
> I was not specting an anonymous subclassing.
Your amb is an Ambiguous, except that you gave it a constructor
that does a bit more than the Ambiguous constructor. This can
only be instantiated as an instance of a subclass of Ambiguous
that has this modified constructor and is anonymous, as you gave
it no name.
- Florian.