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

case classes and auxiliary constructors

2 replies
wookietreiber
Joined: 2011-04-24,
User offline. Last seen 42 years 45 weeks ago.

hi _

I just discovered that the compiler does not create "auxiliary" apply methods
for auxiliary constructors of case classes, as in:

scala> case class Mod(f: Int => Int) {
| def this(i: Int) = this(Int => i)
| }
defined class Mod

scala> Mod(20)
:19: error: type mismatch;
found : Int(20)
required: (Int) => Int
Mod(20)
^

why is that? is there a reason, why this feature is not implemented or has no
one had the time yet to do it?

the auxiliary apply method syntax is:

def apply(argsOfAuxiliaryConstructor) = new Foo(argsOfAuxiliaryConstructor)

can/will this be implemented?

best regards
christian krause aka wookietreiber

sullivan-
Joined: 2009-10-13,
User offline. Last seen 39 weeks 5 days ago.
Re: case classes and auxiliary constructors
Hi Christian,
I have no answers for any of your questions, but I will note that you can provide the auxiliary factory method yourself quite easily:
object Mod {  def apply(i: Int) = new Mod(i) // <----- DIY 1-liner -----   def main(args: Array[String]) = {    val sampleMod = Mod(7)    println("sampleMod = " + sampleMod)  }}case class Mod(f: Int => Int) {  def this(i: Int) = this(Int => i) }
Running this program gives me the following output:
sampleMod = Mod(<function1>)
This doesn't work well in the REPL. I'm not exactly sure why. Other people on the list might be able to tell you.
Thinking it over, one possible reason why the auxiliary apply method is not provided is that if it were, people might also expect a corresponding unapply method, which is probably quite a bit more complicated than the one-liner above. (But that's just speculation.)
Best, John

On Mon, Jul 11, 2011 at 5:28 AM, wookietreiber <kizkizzbangbang [at] googlemail [dot] com> wrote:
hi _

I just discovered that the compiler does not create "auxiliary" apply methods
for auxiliary constructors of case classes, as in:

scala> case class Mod(f: Int => Int) {
    |   def this(i: Int) = this(Int => i)
    | }
defined class Mod

scala> Mod(20)
<console>:19: error: type mismatch;
 found   : Int(20)
 required: (Int) => Int
      Mod(20)
          ^

why is that? is there a reason, why this feature is not implemented or has no
one had the time yet to do it?

the auxiliary apply method syntax is:

def apply(argsOfAuxiliaryConstructor) = new Foo(argsOfAuxiliaryConstructor)

can/will this be implemented?

best regards
christian krause aka wookietreiber




--
There are more things in heaven and earth, Horatio,
Than are dreamt of in your philosophy.
ichoran
Joined: 2009-08-14,
User offline. Last seen 2 years 3 weeks ago.
Re: case classes and auxiliary constructors
It works fine in the REPL _if you enter all the code in one block_.  Otherwise it treats the two as independent: creation of an object, and then creation of an independent case class that happens to have the same name.  In 2.9 you can use :paste to enter a block; in earlier versions, you can enclose both in a wrapper object.

  --Rex

On Mon, Jul 11, 2011 at 8:52 AM, john sullivan <sullymandias [at] gmail [dot] com> wrote:
This doesn't work well in the REPL. I'm not exactly sure why. Other people on the list might be able to tell you.


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