Case class companion objects extending FunctionN in line with SLS?
Andrew Phillips <[email protected]> Wed, 14 Dec 2016 21:19:44 -0800 (PST)
| Newsgroups | gmane.comp.lang.scala |
|---|---|
| Message-ID | <[email protected]> |
------=_Part_64_1001511241.1481779184799
Content-Type: multipart/alternative;
boundary="----=_Part_65_342285066.1481779184799"
------=_Part_65_342285066.1481779184799
Content-Type: text/plain; charset=UTF-8
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.
------=_Part_65_342285066.1481779184799
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div>Hi all</div><div><br></div><div>As far as I can see f=
rom usage and <font face=3D"courier new, monospace">-print</font> REPL outp=
ut, the generated companion object for a case class with N parameters is a =
subclass of <font face=3D"courier new, monospace">FunctionN</font> (2.11.6 =
REPL):</div><div><br></div><div><font face=3D"courier new, monospace">scala=
> :set -print</font></div><div><font face=3D"courier new, monospace"><br=
></font></div><div><font face=3D"courier new, monospace">scala> case cla=
ss Foo()</font></div><div><font face=3D"courier new, monospace">[[syntax tr=
ees 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><font face=3D"courier new=
, monospace">...</font></div><div><font face=3D"courier new, monospace">=C2=
=A0 <synthetic> object iw$Foo extends scala.runtime.AbstractFunction0=
with Serializable {</font></div><div><font face=3D"courier new, monospace"=
><br></font></div><div><font face=3D"courier new, monospace">scala> case=
class Bar(n: Int)</font></div><div><font face=3D"courier new, monospace">[=
[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><font face=3D"c=
ourier new, monospace">...</font></div><div><font face=3D"courier new, mono=
space">=C2=A0 <synthetic> object iw$Bar extends scala.runtime.Abstrac=
tFunction1 with Serializable {</font></div><div><font face=3D"courier new, =
monospace"><br></font></div><div><font face=3D"courier new, monospace">scal=
a> List(1, 2) map Bar</font></div><div><font face=3D"courier new, monosp=
ace">res0: List[Bar] =3D List(Bar(1), Bar(2))</font></div><div><br></div><d=
iv>SLS 5.3.2 [1] does not, as far as I can make out, say anything about thi=
s. The extractor object as defined there behaves differently from the one g=
enerate by the compiler:</div><div><br></div><div><font face=3D"courier new=
, monospace">scala> :paste</font></div><div><font face=3D"courier new, m=
onospace">// Entering paste mode (ctrl-D to finish)</font></div><div><font =
face=3D"courier new, monospace"><br></font></div><div><font face=3D"courier=
new, monospace">// not a case class; define the extractor as described in =
SLS explicitly</font></div><div><font face=3D"courier new, monospace">class=
Baz(n: Int)</font></div><div><font face=3D"courier new, monospace">object =
Baz {</font></div><div><font face=3D"courier new, monospace">=C2=A0 def app=
ly(n: Int): Baz =3D new Baz(n)</font></div><div><font face=3D"courier new, =
monospace">=C2=A0 // other stuff</font></div><div><font face=3D"courier new=
, monospace">}</font></div><div><font face=3D"courier new, monospace"><br><=
/font></div><div><font face=3D"courier new, monospace">// Exiting paste mod=
e, now interpreting.</font></div><div><font face=3D"courier new, monospace"=
><br></font></div><div><font face=3D"courier new, monospace">defined class =
Baz</font></div><div><font face=3D"courier new, monospace">defined object B=
az</font></div><div><font face=3D"courier new, monospace"><br></font></div>=
<div><font face=3D"courier new, monospace">scala> List(1, 2) map Baz</fo=
nt></div><div><font face=3D"courier new, monospace"><console>:12: err=
or: type mismatch;</font></div><div><font face=3D"courier new, monospace">=
=C2=A0found =C2=A0 : Baz.type</font></div><div><font face=3D"courier new, m=
onospace">=C2=A0required: Int =3D> ?</font></div><div><font face=3D"cour=
ier new, monospace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 List(1=
, 2) map Baz</font></div><div><font face=3D"courier new, monospace">=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>Is this a misreading/-understand=
ing? If not, would this be considered a compiler bug, a spec omission or so=
mething else?<br></div><div><br></div><div>Regards</div><div><br></div><div=
>ap</div><div><br></div><div>[1] https://www.scala-lang.org/files/archive/s=
pec/2.13/05-classes-and-objects.html#case-classes</div></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 />
------=_Part_65_342285066.1481779184799--
------=_Part_64_1001511241.1481779184799--