This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Ambiguous Constructors

1 reply
Andrés Testi
Joined: 2009-01-22,
User offline. Last seen 42 years 45 weeks ago.

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

Florian Hars
Joined: 2008-12-18,
User offline. Last seen 42 years 45 weeks ago.
Re: Ambiguous Constructors

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.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland