Re: Is it a bug? Implicit map interferes with nonEmpty call
Andrew Phillips <[email protected]> Thu, 27 Oct 2016 05:52:24 -0700 (PDT)
| Newsgroups | gmane.comp.lang.scala |
|---|---|
| Message-ID | <[email protected]> |
------=_Part_190_661323872.1477572744863 Content-Type: multipart/alternative; boundary="----=_Part_191_1606602860.1477572744863" ------=_Part_191_1606602860.1477572744863 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hi Jasper *> This is a case where a warning is actually necessary because it is not= =20 obvious that you're introducing an implicit conversion* Would you define "obvious introductions" as being "implicit defs only", or= =20 would you include other options too? Since Scala generally treats defs and values of type Function1=20 interchangeably, it seems consistent to me to also regard any implicit=20 value of type Function1 as being an "intended" implicit conversion. Or=20 rather, the idea of potentially introducing a distinction between defs and= =20 Function1 values for implicits only seems inconsistent. Should a possible warning in this case only warn on Function1 subtypes, or= =20 do you have some other way of marking implicits as "intentional" in mind? Regards ap PS: Another occurrence of this: http://scalapuzzlers.com/#pzzlr-054 On Thursday, 27 October 2016 03:02:06 UTC-4, Jasper-M wrote: > > > > Op donderdag 27 oktober 2016 02:27:53 UTC+2 schreef rkuhn: >> >> Hi Gallus, >> >> this is not a bug, although it is not intuitive. One advice that removes= =20 >> a large class of such surprises is that you should never use implicits o= f=20 >> primitive types or common collections thereof=E2=80=94the risk for havin= g these=20 >> picked up in the wrong context is just too high. In this case, since Map= =20 >> extends Function1 your implicit value is not innocent at all, because it= =20 >> presents another way how to turn a String into something that has a=20 >> nonEmpty method. >> >> One thing that might be considered a bug is that declaring this value=20 >> does not raise an implicit conversion warning=E2=80=94normally such dang= erous=20 >> activities are protected by a language feature import. >> >> > I definitely consider that a bug. This is a case where a warning is=20 > actually necessary because it is not obvious that you're introducing an= =20 > implicit conversion, but you don't get a warning. While if you explicitly= =20 > write an implicit conversion you do get one. > =20 > >> Regards, >> >> Roland >> >> 22 okt. 2016 kl. 14:53 skrev Gallus Anonimus <[email protected]>: >> >> Hi, >> In following code: >> >> object TheApp { >> =20 >> def main(args : Array[String]) { >> implicit val innocentMap =3D Map("foo" -> Set("bar")) >> "baz".nonEmpty >> } >> =20 >> } >> instead of computing expected true as "baz" string is non empty >> results in "java.util.NoSuchElementException: key not found: baz" >> I tested it on 2.10, 2.11.8, 2.12.RC2 with same results. >> The implicit innocentMap is the culprit. >> After decompiling "baz".nonEmpty looks like this: >> ((TraversableOnce)innocentMap.apply("baz")).nonEmpty(); >> >> Can anyone tell me why innocentMap.apply is magically called here? >> Highly unexpected behaviour. If innocentMap has different type like: >> implicit val innocentMap =3D Map("foo" -> ("bar")) >> code behaves as expected. >> >> Sholud I report scala library bug? >> >> --=20 >> You received this message because you are subscribed to the Google Group= s=20 >> "scala-language" group. >> To unsubscribe from this group and stop receiving emails from it, send a= n=20 >> 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. ------=_Part_191_1606602860.1477572744863 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr">Hi Jasper<div><br></div><div><i>> This is a case where = a warning is actually necessary because it is not obvious that you're i= ntroducing an implicit conversion</i></div><div><br></div><div>Would you de= fine "obvious introductions" as being "implicit defs only&qu= ot;, or would you include other options too?</div><div><br></div><div>Since= Scala generally treats <font face=3D"courier new, monospace">defs</font> a= nd values of type <font face=3D"courier new, monospace">Function1</font> in= terchangeably, it seems consistent to me to also regard any implicit value = of type <font face=3D"courier new, monospace">Function1</font> as being an = "intended" implicit conversion. Or rather, the idea of potentiall= y introducing a distinction between <font face=3D"courier new, monospace">d= ef</font>s and <font face=3D"courier new, monospace">Function1</font> value= s for implicits only seems inconsistent.</div><div><br></div><div>Should a = possible warning in this case only warn on <font face=3D"courier new, monos= pace">Function1</font> subtypes, or do you have some other way of marking i= mplicits as "intentional" in mind?</div><div><br></div><div>Regar= ds</div><div><br></div><div>ap</div><div><br></div><div>PS: Another occurre= nce of this:=C2=A0http://scalapuzzlers.com/#pzzlr-054</div><div><br></div><= div><br></div><div><br>On Thursday, 27 October 2016 03:02:06 UTC-4, Jasper-= M wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: = 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr"><br>= <br>Op donderdag 27 oktober 2016 02:27:53 UTC+2 schreef rkuhn:<blockquote c= lass=3D"gmail_quote" style=3D"margin:0;margin-left:0.8ex;border-left:1px #c= cc solid;padding-left:1ex"><div style=3D"word-wrap:break-word">Hi Gallus,<d= iv><br></div><div>this is not a bug, although it is not intuitive. One advi= ce that removes a large class of such surprises is that you should never us= e implicits of primitive types or common collections thereof=E2=80=94the ri= sk for having these picked up in the wrong context is just too high. In thi= s case, since Map extends Function1 your implicit value is not innocent at = all, because it presents another way how to turn a String into something th= at has a nonEmpty method.</div><div><br></div><div>One thing that might be = considered a bug is that declaring this value does not raise an implicit co= nversion warning=E2=80=94normally such dangerous activities are protected b= y a language feature import.</div><div><br></div></div></blockquote><div><b= r></div><div>I definitely consider that a bug. This is a case where a warni= ng is actually necessary because it is not obvious that you're introduc= ing an implicit conversion, but you don't get a warning. While if you e= xplicitly write an implicit conversion you do get one.</div><div>=C2=A0</di= v><blockquote class=3D"gmail_quote" style=3D"margin:0;margin-left:0.8ex;bor= der-left:1px #ccc solid;padding-left:1ex"><div style=3D"word-wrap:break-wor= d"><div></div><div>Regards,</div><div><br></div><div>Roland</div><div><br><= div><blockquote type=3D"cite"><div>22 okt. 2016 kl. 14:53 skrev Gallus Anon= imus <<a rel=3D"nofollow">[email protected]</a>>:</div><br><div><div di= r=3D"ltr">Hi,<br>In following code:<br><br><div style=3D"background-color:r= gb(250,250,250);border-color:rgb(187,187,187);border-style:solid;border-wid= th:1px"><code><div><span style=3D"color:#008">object</span><span> </span><s= pan style=3D"color:#606">TheApp</span><span> </span><span style=3D"color:#6= 60">{</span><span><br>=C2=A0 <br>=C2=A0 </span><span style=3D"color:#008">d= ef</span><span> main</span><span style=3D"color:#660">(</span><span>args </= span><span style=3D"color:#660">:</span><span> </span><span style=3D"color:= #606">Array</span><span style=3D"color:#660">[</span><span style=3D"color:#= 606">String</span><span style=3D"color:#660">])</span><span> </span><span s= tyle=3D"color:#660">{</span><span><br>=C2=A0 =C2=A0 </span><span style=3D"c= olor:#008">implicit</span><span> val innocentMap </span><span style=3D"colo= r:#660">=3D</span><span> </span><span style=3D"color:#606">Map</span><span = style=3D"color:#660">(</span><span style=3D"color:#080">"foo"</sp= an><span> </span><span style=3D"color:#660">-></span><span> </span><span= style=3D"color:#606">Set</span><span style=3D"color:#660">(</span><span st= yle=3D"color:#080">"bar"</span><span style=3D"color:#660">))</spa= n><span><br>=C2=A0 =C2=A0 </span><span style=3D"color:#080">"baz"= </span><span style=3D"color:#660">.</span><span>nonEmpty<br>=C2=A0 </span><= span style=3D"color:#660">}</span><span><br>=C2=A0 <br></span><span style= =3D"color:#660">}</span><span><br></span></div></code></div>instead of comp= uting expected true as "baz" string is non empty<br>results in &q= uot;java.util.<wbr>NoSuchElementException: key not found: baz"<code><s= pan><br></span></code>I tested it on 2.10, 2.11.8, 2.12.RC2 with same resul= ts.<br>The implicit innocentMap is the culprit.<br>After decompiling "= baz".nonEmpty looks like this:<br><div style=3D"background-color:rgb(2= 50,250,250);border-color:rgb(187,187,187);border-style:solid;border-width:1= px"><code><div><span style=3D"color:#660">((</span><span style=3D"color:#60= 6">TraversableOnce</span><span style=3D"color:#660">)</span><span>innocentM= ap</span><span style=3D"color:#660">.</span><span><wbr>apply</span><span st= yle=3D"color:#660">(</span><span style=3D"color:#080">"baz"</span= ><span style=3D"color:#660">)).</span><span>nonEmpty</span><span style=3D"c= olor:#660">();</span></div></code></div><br>Can anyone tell me why innocent= Map.apply is magically called here?<br>Highly unexpected behaviour. If inno= centMap has different type like:<br><div style=3D"background-color:rgb(250,= 250,250);border-color:rgb(187,187,187);border-style:solid;border-width:1px"= ><code><div><span style=3D"color:#008">implicit</span><span> val innocentMa= p </span><span style=3D"color:#660">=3D</span><span> </span><span style=3D"= color:#606">Map</span><span style=3D"color:#660">(</span><span style=3D"col= or:#080">"foo"</span><span> </span><span style=3D"color:#660">-&g= t;</span><span> </span><span style=3D"color:#660">(</span><span style=3D"co= lor:#080">"bar"</span><span style=3D"color:#660">))</span></div><= /code></div>code behaves as expected.<br><br>Sholud I report scala library = bug?<br></div><div><br></div> -- <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 rel=3D"nofollow">scala-languag...@googlegroups.<wbr>com</a>.<br> For more options, visit <a href=3D"https://groups.google.com/d/optout" rel= =3D"nofollow" target=3D"_blank" onmousedown=3D"this.href=3D'https://gro= ups.google.com/d/optout';return true;" onclick=3D"this.href=3D'http= s://groups.google.com/d/optout';return true;">https://groups.google.com= /d/<wbr>optout</a>.<br> </div></blockquote></div><br></div></div></blockquote></div></blockquote></= 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_191_1606602860.1477572744863-- ------=_Part_190_661323872.1477572744863--