Skip to content
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

Wrongly converts classes with multiple constructors #73

Open
OlivierBlanvillain opened this issue Feb 16, 2014 · 5 comments
Open

Wrongly converts classes with multiple constructors #73

OlivierBlanvillain opened this issue Feb 16, 2014 · 5 comments

Comments

@OlivierBlanvillain
Copy link

Here is an example:

class A {
    public A() {
        System.out.println(1);
    }
    public A(String s) {
        System.out.println(2);
    }
}

Which is converted to:

class A {
  println(1)
  def this(s: String) {
    this()
    println(2)
  }
}

Note that it might be impossible to perfectly match Java behavior, especially when inheritance is involved, see http://stackoverflow.com/questions/3299776/in-scala-how-can-i-subclass-a-java-class-with-multiple-constructors.

@timowest
Copy link
Owner

Sorry, this kind of delegation is the best that can be done.

@OlivierBlanvillain
Copy link
Author

What about something like this? It does not look very nice but at least it's behavior equivalent:

class A private (n: Nothing) {
  def this() {
    this(null)
    println(1)
  }
  def this(s: String) {
    this(null)
    println(2)
  }
}

@timowest
Copy link
Owner

Hm, this is an interesting approach, I will consider it.

@timowest timowest reopened this Feb 16, 2014
@timowest timowest removed the wontfix label Feb 16, 2014
@erikkaplun
Copy link

@OlivierBlanvillain: why is the current behavior wrong? the default constructor shouldn't be declared explicitly in Scala... I would be very disappointed if Scala wrapped the default constructor in a def this().

@timowest
Copy link
Owner

I think the point is that if the behaviour can't be solved with constructor delegation then a common delegation target could be used.

This is again a tradeoff between correctness of the result vs having a scalaesque result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants