1

The class definition with compiling error:

class Foo(implicit x : Int) {
  def this(s : String) = this(s.length)
}

and the class definition can pass compiling

class Foo(implicit x : Int) {
  def this(s : String) = this()(s.length)
}

from my point view, the first definition is just correct. Since the auxiliary constructor explicitly called the primary constructor which expected a Integer as parameter, seems nothing wrong. And for the second class definition which pass compiling, actually I don't quite understand why it works.

mfirry
  • 3,634
  • 1
  • 26
  • 36
Haiyuan Zhang
  • 40,802
  • 41
  • 107
  • 134
  • seems like compiler hadn't generated primary constructor in your first example and can't resolve call to `Foo(Int)` – goral Jun 04 '14 at 08:58

1 Answers1

1

In Scala every auxiliary constructor must invoke another constructor of the same class as its first action.

Also, you do not have a constructor with an Int parameter. You have constructor with an implicit (Int) parameter. You can either provide the implicit parameter in the scope or pass it explicitly writing something like this()(9)

mfirry
  • 3,634
  • 1
  • 26
  • 36
  • thanks, but what does this()(9) comes from? or I need to just take it as a fact. actually I searched the scala documentation and Nothing related to this strange syntax. – Haiyuan Zhang Jun 04 '14 at 09:59