Re: Case class companion objects extending FunctionN in line with SLS?
Adriaan Moors <[email protected]> Wed, 21 Dec 2016 00:46:21 +0000
| Newsgroups | gmane.comp.lang.scala |
|---|---|
| Message-ID | <CA+cgcwYdCDWYPT6-+2Dx=U2k-7-tBFRbzk60Y-4ZYtBbMsMSMQ@mail.gmail.com> |
--001a114239ceeee49b0544207b75 Content-Type: text/plain; charset=UTF-8 It's something else, that :-) See also https://issues.scala-lang.org/browse/SI-3664, https://issues.scala-lang.org/browse/SI-5570 On Wed, Dec 14, 2016 at 9:19 PM Andrew Phillips <[email protected]> wrote: > Hi all > > As far as I can see from usage and -print REPL output, the generated > companion object for a case class with N parameters is a subclass of > FunctionN (2.11.6 REPL): > > scala> :set -print > > scala> case class Foo() > [[syntax trees at end of cleanup]] // <console> > ... > <synthetic> object iw$Foo extends scala.runtime.AbstractFunction0 with > Serializable { > > scala> case class Bar(n: Int) > [[syntax trees at end of cleanup]] // <console> > ... > <synthetic> object iw$Bar extends scala.runtime.AbstractFunction1 with > Serializable { > > scala> List(1, 2) map Bar > res0: List[Bar] = List(Bar(1), Bar(2)) > > SLS 5.3.2 [1] does not, as far as I can make out, say anything about this. > The extractor object as defined there behaves differently from the one > generate by the compiler: > > scala> :paste > // Entering paste mode (ctrl-D to finish) > > // not a case class; define the extractor as described in SLS explicitly > class Baz(n: Int) > object Baz { > def apply(n: Int): Baz = new Baz(n) > // other stuff > } > > // Exiting paste mode, now interpreting. > > defined class Baz > defined object Baz > > scala> List(1, 2) map Baz > <console>:12: error: type mismatch; > found : Baz.type > required: Int => ? > List(1, 2) map Baz > ^ > Is this a misreading/-understanding? If not, would this be considered a > compiler bug, a spec omission or something else? > > Regards > > ap > > [1] > https://www.scala-lang.org/files/archive/spec/2.13/05-classes-and-objects.html#case-classes > > -- > You received this message because you are subscribed to the Google Groups > "scala-language" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "scala-language" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout. --001a114239ceeee49b0544207b75 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr">It's something else, that :-)<div><br></div><div>See a= lso <a href=3D"https://issues.scala-lang.org/browse/SI-3664">https://issues= .scala-lang.org/browse/SI-3664</a>,=C2=A0<a href=3D"https://issues.scala-la= ng.org/browse/SI-5570">https://issues.scala-lang.org/browse/SI-5570</a></di= v><div><br></div><div><br></div></div><br><div class=3D"gmail_quote"><div d= ir=3D"ltr">On Wed, Dec 14, 2016 at 9:19 PM Andrew Phillips <<a href=3D"m= ailto:[email protected]">[email protected]</a>> wrote:<br></div><b= lockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px = #ccc solid;padding-left:1ex"><div dir=3D"ltr" class=3D"gmail_msg"><div clas= s=3D"gmail_msg">Hi all</div><div class=3D"gmail_msg"><br class=3D"gmail_msg= "></div><div class=3D"gmail_msg">As far as I can see from usage and <font f= ace=3D"courier new, monospace" class=3D"gmail_msg">-print</font> REPL outpu= t, the generated companion object for a case class with N parameters is a s= ubclass of <font face=3D"courier new, monospace" class=3D"gmail_msg">Functi= onN</font> (2.11.6 REPL):</div><div class=3D"gmail_msg"><br class=3D"gmail_= msg"></div><div class=3D"gmail_msg"><font face=3D"courier new, monospace" c= lass=3D"gmail_msg">scala> :set -print</font></div><div class=3D"gmail_ms= g"><font face=3D"courier new, monospace" class=3D"gmail_msg"><br class=3D"g= mail_msg"></font></div><div class=3D"gmail_msg"><font face=3D"courier new, = monospace" class=3D"gmail_msg">scala> case class Foo()</font></div><div = class=3D"gmail_msg"><font face=3D"courier new, monospace" class=3D"gmail_ms= g">[[syntax trees at end of =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 cleanup]] // <console></font></div><div class=3D"gm= ail_msg"><font face=3D"courier new, monospace" class=3D"gmail_msg">...</fon= t></div><div class=3D"gmail_msg"><font face=3D"courier new, monospace" clas= s=3D"gmail_msg">=C2=A0 <synthetic> object iw$Foo extends scala.runtim= e.AbstractFunction0 with Serializable {</font></div><div class=3D"gmail_msg= "><font face=3D"courier new, monospace" class=3D"gmail_msg"><br class=3D"gm= ail_msg"></font></div><div class=3D"gmail_msg"><font face=3D"courier new, m= onospace" class=3D"gmail_msg">scala> case class Bar(n: Int)</font></div>= <div class=3D"gmail_msg"><font face=3D"courier new, monospace" class=3D"gma= il_msg">[[syntax trees at end of =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 cleanup]] // <console></font></div><div class=3D= "gmail_msg"><font face=3D"courier new, monospace" class=3D"gmail_msg">...</= font></div><div class=3D"gmail_msg"><font face=3D"courier new, monospace" c= lass=3D"gmail_msg">=C2=A0 <synthetic> object iw$Bar extends scala.run= time.AbstractFunction1 with Serializable {</font></div><div class=3D"gmail_= msg"><font face=3D"courier new, monospace" class=3D"gmail_msg"><br class=3D= "gmail_msg"></font></div><div class=3D"gmail_msg"><font face=3D"courier new= , monospace" class=3D"gmail_msg">scala> List(1, 2) map Bar</font></div><= div class=3D"gmail_msg"><font face=3D"courier new, monospace" class=3D"gmai= l_msg">res0: List[Bar] =3D List(Bar(1), Bar(2))</font></div><div class=3D"g= mail_msg"><br class=3D"gmail_msg"></div><div class=3D"gmail_msg">SLS 5.3.2 = [1] does not, as far as I can make out, say anything about this. The extrac= tor object as defined there behaves differently from the one generate by th= e compiler:</div><div class=3D"gmail_msg"><br class=3D"gmail_msg"></div><di= v class=3D"gmail_msg"><font face=3D"courier new, monospace" class=3D"gmail_= msg">scala> :paste</font></div><div class=3D"gmail_msg"><font face=3D"co= urier new, monospace" class=3D"gmail_msg">// Entering paste mode (ctrl-D to= finish)</font></div><div class=3D"gmail_msg"><font face=3D"courier new, mo= nospace" class=3D"gmail_msg"><br class=3D"gmail_msg"></font></div><div clas= s=3D"gmail_msg"><font face=3D"courier new, monospace" class=3D"gmail_msg">/= / not a case class; define the extractor as described in SLS explicitly</fo= nt></div><div class=3D"gmail_msg"><font face=3D"courier new, monospace" cla= ss=3D"gmail_msg">class Baz(n: Int)</font></div><div class=3D"gmail_msg"><fo= nt face=3D"courier new, monospace" class=3D"gmail_msg">object Baz {</font><= /div><div class=3D"gmail_msg"><font face=3D"courier new, monospace" class= =3D"gmail_msg">=C2=A0 def apply(n: Int): Baz =3D new Baz(n)</font></div><di= v class=3D"gmail_msg"><font face=3D"courier new, monospace" class=3D"gmail_= msg">=C2=A0 // other stuff</font></div><div class=3D"gmail_msg"><font face= =3D"courier new, monospace" class=3D"gmail_msg">}</font></div><div class=3D= "gmail_msg"><font face=3D"courier new, monospace" class=3D"gmail_msg"><br c= lass=3D"gmail_msg"></font></div><div class=3D"gmail_msg"><font face=3D"cour= ier new, monospace" class=3D"gmail_msg">// Exiting paste mode, now interpre= ting.</font></div><div class=3D"gmail_msg"><font face=3D"courier new, monos= pace" class=3D"gmail_msg"><br class=3D"gmail_msg"></font></div><div class= =3D"gmail_msg"><font face=3D"courier new, monospace" class=3D"gmail_msg">de= fined class Baz</font></div><div class=3D"gmail_msg"><font face=3D"courier = new, monospace" class=3D"gmail_msg">defined object Baz</font></div><div cla= ss=3D"gmail_msg"><font face=3D"courier new, monospace" class=3D"gmail_msg">= <br class=3D"gmail_msg"></font></div><div class=3D"gmail_msg"><font face=3D= "courier new, monospace" class=3D"gmail_msg">scala> List(1, 2) map Baz</= font></div><div class=3D"gmail_msg"><font face=3D"courier new, monospace" c= lass=3D"gmail_msg"><console>:12: error: type mismatch;</font></div><d= iv class=3D"gmail_msg"><font face=3D"courier new, monospace" class=3D"gmail= _msg">=C2=A0found =C2=A0 : Baz.type</font></div><div class=3D"gmail_msg"><f= ont face=3D"courier new, monospace" class=3D"gmail_msg">=C2=A0required: Int= =3D> ?</font></div><div class=3D"gmail_msg"><font face=3D"courier new, = monospace" class=3D"gmail_msg">=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 List(1, 2) map Baz</font></div><div class=3D"gmail_msg"><font face= =3D"courier new, monospace" class=3D"gmail_msg">=C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0^</font></div><div class=3D"gmail_msg">Is this a misreading/-understandi= ng? If not, would this be considered a compiler bug, a spec omission or som= ething else?<br class=3D"gmail_msg"></div><div class=3D"gmail_msg"><br clas= s=3D"gmail_msg"></div><div class=3D"gmail_msg">Regards</div><div class=3D"g= mail_msg"><br class=3D"gmail_msg"></div><div class=3D"gmail_msg">ap</div><d= iv class=3D"gmail_msg"><br class=3D"gmail_msg"></div><div class=3D"gmail_ms= g">[1] <a href=3D"https://www.scala-lang.org/files/archive/spec/2.13/05-cla= sses-and-objects.html#case-classes" class=3D"gmail_msg" target=3D"_blank">h= ttps://www.scala-lang.org/files/archive/spec/2.13/05-classes-and-objects.ht= ml#case-classes</a></div></div> <p class=3D"gmail_msg"></p> -- <br class=3D"gmail_msg"> You received this message because you are subscribed to the Google Groups &= quot;scala-language" group.<br class=3D"gmail_msg"> To unsubscribe from this group and stop receiving emails from it, send an e= mail to <a href=3D"mailto:[email protected]" clas= s=3D"gmail_msg" target=3D"_blank">[email protected]= om</a>.<br class=3D"gmail_msg"> For more options, visit <a href=3D"https://groups.google.com/d/optout" clas= s=3D"gmail_msg" target=3D"_blank">https://groups.google.com/d/optout</a>.<b= r class=3D"gmail_msg"> </blockquote></div> <p></p> -- <br /> You received this message because you are subscribed to the Google Groups &= quot;scala-language" group.<br /> To unsubscribe from this group and stop receiving emails from it, send an e= mail to <a href=3D"mailto:[email protected]">scal= [email protected]</a>.<br /> For more options, visit <a href=3D"https://groups.google.com/d/optout">http= s://groups.google.com/d/optout</a>.<br /> --001a114239ceeee49b0544207b75--