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

Roland Kuhn <[email protected]> Sat, 22 Oct 2016 15:15:58 +0200
Newsgroups gmane.comp.lang.scala
Message-ID <[email protected]>
--Apple-Mail=_883B920E-7778-479B-86C7-13E1CBC1902D
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=UTF-8

Hi Gallus,

this is not a bug, although it is not intuitive. One advice that removes a =
large class of such surprises is that you should never use implicits of pri=
mitive types or common collections thereof=E2=80=94the risk for having thes=
e picked up in the wrong context is just too high. In this case, since Map =
extends Function1 your implicit value is not innocent at all, because it pr=
esents another way how to turn a String into something that has a nonEmpty =
method.

One thing that might be considered a bug is that declaring this value does =
not raise an implicit conversion warning=E2=80=94normally such dangerous ac=
tivities are protected by a language feature import.

Regards,

Roland

> 22 okt. 2016 kl. 14:53 skrev Gallus Anonimus <[email protected]>:
>=20
> Hi,
> In following code:
>=20
> 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();
>=20
> 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.
>=20
> Sholud I report scala library bug?
>=20
> --=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=
 email to [email protected] <mailto:scala-languag=
[email protected]>.
> For more options, visit https://groups.google.com/d/optout <https://group=
s.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.

--Apple-Mail=_883B920E-7778-479B-86C7-13E1CBC1902D
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset=UTF-8

<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html charset=
=3Dutf-8"></head><body style=3D"word-wrap: break-word; -webkit-nbsp-mode: s=
pace; -webkit-line-break: after-white-space;" class=3D"">Hi Gallus,<div cla=
ss=3D""><br class=3D""></div><div class=3D"">this is not a bug, although it=
 is not intuitive. One advice that removes a large class of such surprises =
is that you should never use implicits of primitive types or common collect=
ions thereof=E2=80=94the risk for having these picked up in the wrong conte=
xt is just too high. In this case, since Map extends Function1 your implici=
t 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 class=3D""><=
br class=3D""></div><div class=3D"">One thing that might be considered a bu=
g is that declaring this value does not raise an implicit conversion warnin=
g=E2=80=94normally such dangerous activities are protected by a language fe=
ature import.</div><div class=3D""><br class=3D""></div><div class=3D"">Reg=
ards,</div><div class=3D""><br class=3D""></div><div class=3D"">Roland</div=
><div class=3D""><br class=3D""><div><blockquote type=3D"cite" class=3D""><=
div class=3D"">22 okt. 2016 kl. 14:53 skrev Gallus Anonimus &lt;<a href=3D"=
mailto:[email protected]" class=3D"">[email protected]</a>&gt;:</div><br clas=
s=3D"Apple-interchange-newline"><div class=3D""><div dir=3D"ltr" class=3D""=
>Hi,<br class=3D"">In following code:<br class=3D""><br class=3D""><div sty=
le=3D"background-color: rgb(250, 250, 250); border-color: rgb(187, 187, 187=
); border-style: solid; border-width: 1px; overflow-wrap: break-word;" clas=
s=3D"prettyprint"><code class=3D"prettyprint"><div class=3D"subprettyprint"=
><span style=3D"color: #008;" class=3D"styled-by-prettify">object</span><sp=
an style=3D"" class=3D"styled-by-prettify"> </span><span style=3D"color: #6=
06;" class=3D"styled-by-prettify">TheApp</span><span style=3D"" class=3D"st=
yled-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-p=
rettify">{</span><span style=3D"" class=3D"styled-by-prettify"><br class=3D=
"">&nbsp; <br class=3D"">&nbsp; </span><span style=3D"color: #008;" class=
=3D"styled-by-prettify">def</span><span style=3D"" class=3D"styled-by-prett=
ify"> main</span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
(</span><span style=3D"" class=3D"styled-by-prettify">args </span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">:</span><span style=3D"" c=
lass=3D"styled-by-prettify"> </span><span style=3D"color: #606;" class=3D"s=
tyled-by-prettify">Array</span><span style=3D"color: #660;" class=3D"styled=
-by-prettify">[</span><span style=3D"color: #606;" class=3D"styled-by-prett=
ify">String</span><span style=3D"color: #660;" class=3D"styled-by-prettify"=
>])</span><span style=3D"" class=3D"styled-by-prettify"> </span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">{</span><span style=3D"" cla=
ss=3D"styled-by-prettify"><br class=3D"">&nbsp; &nbsp; </span><span style=
=3D"color: #008;" class=3D"styled-by-prettify">implicit</span><span style=
=3D"" class=3D"styled-by-prettify"> val innocentMap </span><span style=3D"c=
olor: #660;" class=3D"styled-by-prettify">=3D</span><span style=3D"" class=
=3D"styled-by-prettify"> </span><span style=3D"color: #606;" class=3D"style=
d-by-prettify">Map</span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify">(</span><span style=3D"color: #080;" class=3D"styled-by-prettify">"=
foo"</span><span style=3D"" class=3D"styled-by-prettify"> </span><span styl=
e=3D"color: #660;" class=3D"styled-by-prettify">-&gt;</span><span style=3D"=
" class=3D"styled-by-prettify"> </span><span style=3D"color: #606;" class=
=3D"styled-by-prettify">Set</span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">(</span><span style=3D"color: #080;" class=3D"styled-by-pr=
ettify">"bar"</span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">))</span><span style=3D"" class=3D"styled-by-prettify"><br class=3D"">&n=
bsp; &nbsp; </span><span style=3D"color: #080;" class=3D"styled-by-prettify=
">"baz"</span><span style=3D"color: #660;" class=3D"styled-by-prettify">.</=
span><span style=3D"" class=3D"styled-by-prettify">nonEmpty<br class=3D"">&=
nbsp; </span><span style=3D"color: #660;" class=3D"styled-by-prettify">}</s=
pan><span style=3D"" class=3D"styled-by-prettify"><br class=3D"">&nbsp; <br=
 class=3D""></span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">}</span><span style=3D"" class=3D"styled-by-prettify"><br class=3D""></sp=
an></div></code></div>instead of computing expected true as "baz" string is=
 non empty<br class=3D"">results in "java.util.NoSuchElementException: key =
not found: baz"<code class=3D"prettyprint"><span style=3D"" class=3D"styled=
-by-prettify"><br class=3D""></span></code>I tested it on 2.10, 2.11.8, 2.1=
2.RC2 with same results.<br class=3D"">The implicit innocentMap is the culp=
rit.<br class=3D"">After decompiling "baz".nonEmpty looks like this:<br cla=
ss=3D""><div style=3D"background-color: rgb(250, 250, 250); border-color: r=
gb(187, 187, 187); border-style: solid; border-width: 1px; overflow-wrap: b=
reak-word;" class=3D"prettyprint"><code class=3D"prettyprint"><div class=3D=
"subprettyprint"><span style=3D"color: #660;" class=3D"styled-by-prettify">=
((</span><span style=3D"color: #606;" class=3D"styled-by-prettify">Traversa=
bleOnce</span><span style=3D"color: #660;" class=3D"styled-by-prettify">)</=
span><span style=3D"" class=3D"styled-by-prettify">innocentMap</span><span =
style=3D"color: #660;" class=3D"styled-by-prettify">.</span><span style=3D"=
" class=3D"styled-by-prettify">apply</span><span style=3D"color: #660;" cla=
ss=3D"styled-by-prettify">(</span><span style=3D"color: #080;" class=3D"sty=
led-by-prettify">"baz"</span><span style=3D"color: #660;" class=3D"styled-b=
y-prettify">)).</span><span style=3D"" class=3D"styled-by-prettify">nonEmpt=
y</span><span style=3D"color: #660;" class=3D"styled-by-prettify">();</span=
></div></code></div><br class=3D"">Can anyone tell me why innocentMap.apply=
 is magically called here?<br class=3D"">Highly unexpected behaviour. If in=
nocentMap has different type like:<br class=3D""><div style=3D"background-c=
olor: rgb(250, 250, 250); border-color: rgb(187, 187, 187); border-style: s=
olid; border-width: 1px; overflow-wrap: break-word;" class=3D"prettyprint">=
<code class=3D"prettyprint"><div class=3D"subprettyprint"><span style=3D"co=
lor: #008;" class=3D"styled-by-prettify">implicit</span><span style=3D"" cl=
ass=3D"styled-by-prettify"> val innocentMap </span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">=3D</span><span style=3D"" class=3D"style=
d-by-prettify"> </span><span style=3D"color: #606;" class=3D"styled-by-pret=
tify">Map</span><span style=3D"color: #660;" class=3D"styled-by-prettify">(=
</span><span style=3D"color: #080;" class=3D"styled-by-prettify">"foo"</spa=
n><span style=3D"" class=3D"styled-by-prettify"> </span><span style=3D"colo=
r: #660;" class=3D"styled-by-prettify">-&gt;</span><span style=3D"" class=
=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">(</span><span style=3D"color: #080;" class=3D"styled-by-pret=
tify">"bar"</span><span style=3D"color: #660;" class=3D"styled-by-prettify"=
>))</span></div></code></div>code behaves as expected.<br class=3D""><br cl=
ass=3D"">Sholud I report scala library bug?<br class=3D""></div><div class=
=3D""><br class=3D"webkit-block-placeholder"></div>

-- <br class=3D"">
You received this message because you are subscribed to the Google Groups "=
scala-language" group.<br class=3D"">
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"">[email protected]</a>.<br class=3D"">
For more options, visit <a href=3D"https://groups.google.com/d/optout" clas=
s=3D"">https://groups.google.com/d/optout</a>.<br class=3D"">
</div></blockquote></div><br class=3D""></div></body></html>

<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 />

--Apple-Mail=_883B920E-7778-479B-86C7-13E1CBC1902D--