- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Question about case class constructors
Fri, 2009-03-06, 16:27
Hi,
I just stumpled on a problem with case classes.
Why is it possible to use overloaded constructors if you use "new CaseClass()"
but not with "CaseClass()" ?
Here's an example:
scala> case class Test(o:Option[String]) { def this(s:String) = this(Some(s))}
defined class Test
scala> Test("A")
:7: error: type mismatch;
found : java.lang.String("A")
required: Option[String]
Test("A")
^
scala> new Test("A")
res3: Test = Test(Some(A))
Thank you for any insight into this,
Joachim
You can do:
case class Test(o: Option[String])
object Test { def apply(s: String) = new Test(Some(s))}
On Fri, Mar 6, 2009 at 7:25 AM, Joachim Ansorg <nospam@joachim-ansorg.de> wrote:
--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp