Re: Allow top-level "implicit class"es by folding them into containing package-object
Jason Zaugg <[email protected]> Wed, 07 Dec 2016 04:28:55 +0000
| Newsgroups | gmane.comp.lang.scala |
|---|---|
| Message-ID | <CAG3_yeX8t7TuVSzeS6PR7oQbvvify-TAoGhm=tAbJBsMwVqGSw@mail.gmail.com> |
--001a113f8e5c1573a3054309f675 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Unfortunately we have a number of assumptions in our toolchain (mostly driven by the requirement of separate+incremental compilation), that each class file comes from a single source file. Package objects are modelled as an object with the name package, so like any other object they must be defined within a single source file (by convention, named package.scala.) I believe instead that we should allow top level implicit classes and treat it as though it introduced companion object of type Function1[Wrapped, Wrapper]. The companion itself would be seen as an implicit. The call to Wrapper.apply(wrapped) couId be translated to new Wrapper(wrapped) in order to make later optimizations for implicit classes work as expected. We already do this for calls to the companion apply methods of case classes. =E2=80=8B -jason On Wed, 7 Dec 2016 at 13:26 Ryan Williams <[email protected]> wrote: > The requirement that implicit classes be explicitly declared inside > another object gets in my way frequently and feels like it could be > relaxed. > > My typical library code looks like: > > package foo.bar > > object Baz { > implicit class Baz(=E2=80=A6) { =E2=80=A6 } > } > > and corresponding calling code: > > import foo.bar.Baz.Baz > > One of the two Baz-layers is redundant. > > Logically, i want my implicit classes to be considered to live inside > their nearest containing package object (in this case foo.bar), just like > non-implicit classes, but I can only declare a given package object once, > so to use it I have to put all classes in a package in the same file (in > the package object), which is also suboptimal. > > LMK if I'm missing something? Thanks! > > -- > 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. > --=20 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 e= mail to [email protected]. For more options, visit https://groups.google.com/d/optout. --001a113f8e5c1573a3054309f675 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr"><p style=3D"font-size:small;margin:1.2em 0px"></p><div cla= ss=3D"markdown-here-wrapper" style=3D""><p style=3D"margin:1.2em 0px!import= ant">Unfortunately we have a number of assumptions in our toolchain (mostly= driven by the requirement of separate+incremental compilation), that each = class file comes from a single source file. Package objects are modelled as= an object with the name package, so like any other object they must be def= ined within a single source file (by convention, named <code style=3D"font-= size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0= .15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,2= 34);background-color:rgb(248,248,248);border-radius:3px;display:inline">pac= kage.scala</code>.)</p><p style=3D"margin:1.2em 0px!important"><br>I believ= e instead that we should allow top level implicit classes and treat it as t= hough it introduced companion object of type <code style=3D"font-size:0.85e= m;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padd= ing:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);backgr= ound-color:rgb(248,248,248);border-radius:3px;display:inline">Function1[Wra= pped, Wrapper]</code>. The companion itself would be seen as an implicit. T= he call to <code style=3D"font-size:0.85em;font-family:Consolas,Inconsolata= ,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap= ;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border= -radius:3px;display:inline">Wrapper.apply(wrapped)</code> couId be translat= ed to <code style=3D"font-size:0.85em;font-family:Consolas,Inconsolata,Cour= ier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;bord= er:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radi= us:3px;display:inline">new Wrapper(wrapped)</code> in order to make later o= ptimizations for implicit classes work as expected. We already do this for = calls to the companion apply methods of case classes.</p> <div title=3D"MDH:PHAgc3R5bGU9ImZvbnQtc2l6ZTogc21hbGw7IG1hcmdpbjogMS4yZW0gM= HB4OyI+VW5mb3J0dW5h dGVseSB3ZSBoYXZlIGEgbnVtYmVyIG9mIGFzc3VtcHRpb25zIGluIG91ciB0b29sY2hhaW4gKG1= v c3RseSBkcml2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lIGNsYXNzZXM= u PC9wPg=3D=3D" style=3D"height:0;width:0;max-height:0;max-width:0;overflow:h= idden;font-size:0em;padding:0;margin:0">=E2=80=8B</div></div><p style=3D"fo= nt-size:small;margin:1.2em 0px"></p><p style=3D"font-size:small;margin:1.2e= m 0px">-jason</p></div><br><div class=3D"gmail_quote"><div dir=3D"ltr">On W= ed, 7 Dec 2016 at 13:26 Ryan Williams <<a href=3D"mailto:ryan.blake.will= [email protected]">[email protected]</a>> wrote:<br></div><bloc= kquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #cc= c solid;padding-left:1ex"><div dir=3D"ltr" class=3D"gmail_msg"><div class= =3D"gmail_msg">The requirement that implicit classes=C2=A0be explicitly dec= lared inside another <font face=3D"courier new, monospace" class=3D"gmail_m= sg">object</font>=C2=A0gets in my way frequently and feels like it could be= relaxed.</div><div class=3D"gmail_msg"><br class=3D"gmail_msg"></div><div = class=3D"gmail_msg">My typical library code looks like:</div><div class=3D"= gmail_msg"><br class=3D"gmail_msg"></div><div class=3D"m_-34759884949814300= 7prettyprint gmail_msg" style=3D"background-color:rgb(250,250,250);border-c= olor:rgb(187,187,187);border-style:solid;border-width:1px;word-wrap:break-w= ord"><code class=3D"m_-347598849498143007prettyprint gmail_msg"><div class= =3D"m_-347598849498143007subprettyprint gmail_msg"><span style=3D"color:#00= 8" class=3D"m_-347598849498143007styled-by-prettify gmail_msg">package</spa= n><span style=3D"color:#000" class=3D"m_-347598849498143007styled-by-pretti= fy gmail_msg"> foo</span><span style=3D"color:#660" class=3D"m_-34759884949= 8143007styled-by-prettify gmail_msg">.</span><span style=3D"color:#000" cla= ss=3D"m_-347598849498143007styled-by-prettify gmail_msg">bar<br class=3D"gm= ail_msg"><br class=3D"gmail_msg"></span><span style=3D"color:#008" class=3D= "m_-347598849498143007styled-by-prettify gmail_msg">object</span><span styl= e=3D"color:#000" class=3D"m_-347598849498143007styled-by-prettify gmail_msg= "> </span><span style=3D"color:#606" class=3D"m_-347598849498143007styled-b= y-prettify gmail_msg">Baz</span><span style=3D"color:#000" class=3D"m_-3475= 98849498143007styled-by-prettify gmail_msg"> </span><span style=3D"color:#6= 60" class=3D"m_-347598849498143007styled-by-prettify gmail_msg">{</span><sp= an style=3D"color:#000" class=3D"m_-347598849498143007styled-by-prettify gm= ail_msg"><br class=3D"gmail_msg">=C2=A0 </span><span style=3D"color:#008" c= lass=3D"m_-347598849498143007styled-by-prettify gmail_msg">implicit</span><= span style=3D"color:#000" class=3D"m_-347598849498143007styled-by-prettify = gmail_msg"> </span><span style=3D"color:#008" class=3D"m_-34759884949814300= 7styled-by-prettify gmail_msg">class</span><span style=3D"color:#000" class= =3D"m_-347598849498143007styled-by-prettify gmail_msg"> </span><span style= =3D"color:#606" class=3D"m_-347598849498143007styled-by-prettify gmail_msg"= >Baz</span><span style=3D"color:#660" class=3D"m_-347598849498143007styled-= by-prettify gmail_msg">(=E2=80=A6)</span><span style=3D"color:#000" class= =3D"m_-347598849498143007styled-by-prettify gmail_msg"> </span><span style= =3D"color:#660" class=3D"m_-347598849498143007styled-by-prettify gmail_msg"= >{</span><span style=3D"color:#000" class=3D"m_-347598849498143007styled-by= -prettify gmail_msg"> </span><span style=3D"color:#660" class=3D"m_-3475988= 49498143007styled-by-prettify gmail_msg">=E2=80=A6</span><span style=3D"col= or:#000" class=3D"m_-347598849498143007styled-by-prettify gmail_msg"> </spa= n><span style=3D"color:#660" class=3D"m_-347598849498143007styled-by-pretti= fy gmail_msg">}</span><span style=3D"color:#000" class=3D"m_-34759884949814= 3007styled-by-prettify gmail_msg"><br class=3D"gmail_msg"></span><span styl= e=3D"color:#660" class=3D"m_-347598849498143007styled-by-prettify gmail_msg= ">}</span><span style=3D"color:#000" class=3D"m_-347598849498143007styled-b= y-prettify gmail_msg"><br class=3D"gmail_msg"></span></div></code></div><di= v class=3D"gmail_msg"><br class=3D"gmail_msg"></div><div class=3D"gmail_msg= ">and corresponding calling code:</div><div class=3D"gmail_msg"><br class= =3D"gmail_msg"></div><div class=3D"m_-347598849498143007prettyprint gmail_m= sg" style=3D"background-color:rgb(250,250,250);border-color:rgb(187,187,187= );border-style:solid;border-width:1px;word-wrap:break-word"><code class=3D"= m_-347598849498143007prettyprint gmail_msg"><div class=3D"m_-34759884949814= 3007subprettyprint gmail_msg"><span style=3D"color:#008" class=3D"m_-347598= 849498143007styled-by-prettify gmail_msg">import</span><span style=3D"color= :#000" class=3D"m_-347598849498143007styled-by-prettify gmail_msg"> foo</sp= an><span style=3D"color:#660" class=3D"m_-347598849498143007styled-by-prett= ify gmail_msg">.</span><span style=3D"color:#000" class=3D"m_-3475988494981= 43007styled-by-prettify gmail_msg">bar</span><span style=3D"color:#660" cla= ss=3D"m_-347598849498143007styled-by-prettify gmail_msg">.</span><span styl= e=3D"color:#606" class=3D"m_-347598849498143007styled-by-prettify gmail_msg= ">Baz</span><span style=3D"color:#660" class=3D"m_-347598849498143007styled= -by-prettify gmail_msg">.</span><span style=3D"color:#606" class=3D"m_-3475= 98849498143007styled-by-prettify gmail_msg">Baz</span><span style=3D"color:= #000" class=3D"m_-347598849498143007styled-by-prettify gmail_msg"><br class= =3D"gmail_msg"></span></div></code></div><div class=3D"gmail_msg"><br class= =3D"gmail_msg"></div><div class=3D"gmail_msg">One of the two <font face=3D"= courier new, monospace" class=3D"gmail_msg">Baz</font>-layers is redundant.= </div><div class=3D"gmail_msg"><br class=3D"gmail_msg"></div><div class=3D"= gmail_msg">Logically, i want my implicit classes to be considered to live i= nside their nearest containing <font face=3D"courier new, monospace" class= =3D"gmail_msg">package object</font> (in this case <font face=3D"courier ne= w, monospace" class=3D"gmail_msg">foo.bar</font>), just like non-implicit c= lasses, but I can only declare a given <font face=3D"courier new, monospace= " class=3D"gmail_msg">package object</font> once, so to use it I have to pu= t all classes in a package in the same file (in the package object), which = is also suboptimal.</div><div class=3D"gmail_msg"><br class=3D"gmail_msg"><= /div><div class=3D"gmail_msg">LMK if I'm missing something? Thanks!</di= v></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 /> --001a113f8e5c1573a3054309f675--