Re: Is it a bug? Implicit map interferes with nonEmpty call

Jasper-M <[email protected]> Thu, 27 Oct 2016 00:02:05 -0700 (PDT)
Newsgroups gmane.comp.lang.scala
Message-ID <[email protected]>
------=_Part_88_1718406082.1477551725995
Content-Type: multipart/alternative; 
	boundary="----=_Part_89_1417746128.1477551725995"

------=_Part_89_1417746128.1477551725995
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable



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 =
a=20
> large class of such surprises is that you should never use implicits of=
=20
> primitive types or common collections thereof=E2=80=94the risk for having=
 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 doe=
s=20
> not raise an implicit conversion warning=E2=80=94normally such dangerous =
activities=20
> 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]=20
> <javascript:>>:
>
> 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 Groups=
=20
> "scala-language" group.
> To unsubscribe from this group and stop receiving emails from it, send an=
=20
> email to [email protected] <javascript:>.
> 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_89_1417746128.1477551725995
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br><br>Op donderdag 27 oktober 2016 02:27:53 UTC+2 schree=
f rkuhn:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0=
.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div style=3D"word-wra=
p:break-word">Hi Gallus,<div><br></div><div>this is not a bug, although it =
is not intuitive. One advice that removes a large class of such surprises i=
s that you should never use implicits of primitive types or common collecti=
ons thereof=E2=80=94the risk for having these picked up in the wrong contex=
t is just too high. In this 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 that has a nonEmpty method.</div><div><br></div><di=
v>One thing that might be considered a bug is that declaring this value doe=
s not raise an implicit conversion warning=E2=80=94normally such dangerous =
activities are protected by a language feature import.</div><div><br></div>=
</div></blockquote><div><br></div><div>I definitely consider that a bug. Th=
is is a case where a warning is actually necessary because it is not obviou=
s that you&#39;re introducing an implicit conversion, but you don&#39;t get=
 a warning. While if you explicitly write an implicit conversion you do get=
 one.</div><div>=C2=A0</div><blockquote class=3D"gmail_quote" style=3D"marg=
in: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><d=
iv style=3D"word-wrap:break-word"><div></div><div>Regards,</div><div><br></=
div><div>Roland</div><div><br><div><blockquote type=3D"cite"><div>22 okt. 2=
016 kl. 14:53 skrev Gallus Anonimus &lt;<a href=3D"javascript:" target=3D"_=
blank" gdf-obfuscated-mailto=3D"gfCEdFYEBgAJ" rel=3D"nofollow" onmousedown=
=3D"this.href=3D&#39;javascript:&#39;;return true;" onclick=3D"this.href=3D=
&#39;javascript:&#39;;return true;">[email protected]</a>&gt;:</div><br><div>=
<div dir=3D"ltr">Hi,<br>In following code:<br><br><div style=3D"background-=
color:rgb(250,250,250);border-color:rgb(187,187,187);border-style:solid;bor=
der-width:1px"><code><div><span style=3D"color:#008">object</span><span> </=
span><span style=3D"color:#606">TheApp</span><span> </span><span style=3D"c=
olor:#660">{</span><span><br>=C2=A0 <br>=C2=A0 </span><span style=3D"color:=
#008">def</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 style=3D"color:#660">{</span><span><br>=C2=A0 =C2=A0 </span><span sty=
le=3D"color:#008">implicit</span><span> val innocentMap </span><span style=
=3D"color:#660">=3D</span><span> </span><span style=3D"color:#606">Map</spa=
n><span style=3D"color:#660">(</span><span style=3D"color:#080">&quot;foo&q=
uot;</span><span> </span><span style=3D"color:#660">-&gt;</span><span> </sp=
an><span style=3D"color:#606">Set</span><span style=3D"color:#660">(</span>=
<span style=3D"color:#080">&quot;bar&quot;</span><span style=3D"color:#660"=
>))</span><span><br>=C2=A0 =C2=A0 </span><span style=3D"color:#080">&quot;b=
az&quot;</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 o=
f computing expected true as &quot;baz&quot; string is non empty<br>results=
 in &quot;java.util.<wbr>NoSuchElementException: key not found: baz&quot;<c=
ode><span><br></span></code>I tested it on 2.10, 2.11.8, 2.12.RC2 with same=
 results.<br>The implicit innocentMap is the culprit.<br>After decompiling =
&quot;baz&quot;.nonEmpty looks like this:<br><div style=3D"background-color=
:rgb(250,250,250);border-color:rgb(187,187,187);border-style:solid;border-w=
idth:1px"><code><div><span style=3D"color:#660">((</span><span style=3D"col=
or:#606">TraversableOnce</span><span style=3D"color:#660">)</span><span>inn=
ocentMap</span><span style=3D"color:#660">.</span><span><wbr>apply</span><s=
pan style=3D"color:#660">(</span><span style=3D"color:#080">&quot;baz&quot;=
</span><span style=3D"color:#660">)).</span><span>nonEmpty</span><span styl=
e=3D"color:#660">();</span></div></code></div><br>Can anyone tell me why in=
nocentMap.apply is magically called here?<br>Highly unexpected behaviour. I=
f innocentMap has different type like:<br><div style=3D"background-color:rg=
b(250,250,250);border-color:rgb(187,187,187);border-style:solid;border-widt=
h:1px"><code><div><span style=3D"color:#008">implicit</span><span> val inno=
centMap </span><span style=3D"color:#660">=3D</span><span> </span><span sty=
le=3D"color:#606">Map</span><span style=3D"color:#660">(</span><span style=
=3D"color:#080">&quot;foo&quot;</span><span> </span><span style=3D"color:#6=
60">-&gt;</span><span> </span><span style=3D"color:#660">(</span><span styl=
e=3D"color:#080">&quot;bar&quot;</span><span style=3D"color:#660">))</span>=
</div></code></div>code behaves as expected.<br><br>Sholud I report scala l=
ibrary bug?<br></div><div><br></div>

-- <br>
You received this message because you are subscribed to the Google Groups &=
quot;scala-language&quot; group.<br>
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"javascript:" target=3D"_blank" gdf-obfuscated-mailto=3D"=
gfCEdFYEBgAJ" rel=3D"nofollow" onmousedown=3D"this.href=3D&#39;javascript:&=
#39;;return true;" onclick=3D"this.href=3D&#39;javascript:&#39;;return true=
;">scala-languag...@<wbr>googlegroups.com</a>.<br>
For more options, visit <a href=3D"https://groups.google.com/d/optout" targ=
et=3D"_blank" rel=3D"nofollow" onmousedown=3D"this.href=3D&#39;https://grou=
ps.google.com/d/optout&#39;;return true;" onclick=3D"this.href=3D&#39;https=
://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/=
d/<wbr>optout</a>.<br>
</div></blockquote></div><br></div></div></blockquote></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;scala-language&quot; 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_89_1417746128.1477551725995--

------=_Part_88_1718406082.1477551725995--