Can't I define defaults if I define multiple overloaded constructors in Scala?

Taken straight from the compiler's source code: only one overloaded alternative is allowed to define default arguments In general, I wouldn't advise that you mix overloading and defaults. Even if there's no conflict it can make your code harder to read UPDATE Since you added code, it's clear now that you don't want/need to override the default values for each secondary constructor. In your particular case, I might even question the need for those extra constructors at all; Int=>Double is already available for you as an implicit conversion and String=>Double looks like you might be perverting the type system :) Also... As an alternative to overloaded constructors, you can define just the primary constructor with defaults, then overload the apply method of the companion object and use that as a factory.

This is of course completely optional, but it's rapidly becoming established as a pattern through the use of case classes.

Taken straight from the compiler's source code: // only one overloaded alternative is allowed to define default arguments In general, I wouldn't advise that you mix overloading and defaults. Even if there's no conflict it can make your code harder to read. UPDATE Since you added code, it's clear now that you don't want/need to override the default values for each secondary constructor.In your particular case, I might even question the need for those extra constructors at all; Int=>Double is already available for you as an implicit conversion and String=>Double looks like you might be perverting the type system :) Also... As an alternative to overloaded constructors, you can define just the primary constructor with defaults, then overload the apply method of the companion object and use that as a factory.

This is of course completely optional, but it's rapidly becoming established as a pattern through the use of case classes.

Using factories seems pretty unnecessary in the case, and violating Occam's razor. It'd be even prettier to implement constructors for all the cases (manually applying default values) IMHO, and so I've done (looks too oldie and self-repeating though). – Ivan Oct 10 '10 at 10:10 I would take a hard look at my code, if I would need these amounts of constructors.

I hardly need a single one these days... – soc Oct 10 '10 at 14:06.

The overloading fails because you (unnessesarily) define multiple constructors with default values. Do this instead: class A(subject : Double, factor : Int = 1, doItRight : Boolean = true) { def this (subject : Int) = { this(subject. ToDouble) } def this (subject : String) = { this(subject.

ToDouble) } def this () = { this(defaultSubject) } }.

– soc Oct 10 '10 at 14:07 @Ivan, No, you don't need to specify them, as you've already set default values for them in the main constructor. – Banang Oct 10 '10 at 14:54.

In general, I wouldn't advise that you mix overloading and defaults. Even if there's no conflict it can make your code harder to read. Since you added code, it's clear now that you don't want/need to override the default values for each secondary constructor.

Also... As an alternative to overloaded constructors, you can define just the primary constructor with defaults, then overload the apply method of the companion object and use that as a factory. This is of course completely optional, but it's rapidly becoming established as a pattern through the use of case classes.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions