Re: Is it a bug? Implicit map interferes with nonEmpty call
som-snytt <[email protected]> Fri, 4 Nov 2016 23:45:28 -0700 (PDT)
| Newsgroups | gmane.comp.lang.scala |
|---|---|
| Message-ID | <[email protected]> |
------=_Part_978_817057887.1478328329065
Content-Type: multipart/alternative;
boundary="----=_Part_979_859259966.1478328329065"
------=_Part_979_859259966.1478328329065
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
I think there is an old discussion about why there's no warning on the val?=
=20
But I bet a third party lint warning is warranted.
Using def with parens helps:
implicit def innocentMap() =3D Map("foo" -> Set("bar"))
If I were a rapper, I'd be def with parens. Something about living with my=
=20
parents.
On Thursday, October 27, 2016 at 7:47:14 AM UTC-7, Jasper-M wrote:
>
> Hi Andrew,
>
> It's probably best if a warning is given for all implicit conversions (as=
=20
> I thought was supposed to be the case now, unless you import=20
> scala.language.implicitConversions).
> But I think it's highly unlikely that an implicit val or def that has a=
=20
> type (explicit or inferred) that is not Function1 but a subtype thereof=
=20
> (like List[A], Map[A,B]) is intended to be an implicit conversion.
>
> So I would define an "obvious implicit conversion" as any implicit whose=
=20
> static type T =3D:=3D Function1[A,B] or can be eta expanded to a value wi=
th=20
> such a type T.
>
>
> Kinds regards,
> Jasper
>
> Op donderdag 27 oktober 2016 14:52:25 UTC+2 schreef Andrew Phillips:
>>
>> Hi Jasper
>>
>> *> This is a case where a warning is actually necessary because it is no=
t=20
>> obvious that you're introducing an implicit conversion*
>>
>> Would you define "obvious introductions" as being "implicit defs only",=
=20
>> or 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=
=20
>> and Function1 values for implicits only seems inconsistent.
>>
>> Should a possible warning in this case only warn on Function1 subtypes,=
=20
>> or do you have some other way of marking implicits as "intentional" in m=
ind?
>>
>> Regards
>>
>> ap
>>
>> PS: Another occurrence of this: http://scalapuzzlers.com/#pzzlr-054=20
>> <http://www.google.com/url?q=3Dhttp%3A%2F%2Fscalapuzzlers.com%2F%23pzzlr=
-054&sa=3DD&sntz=3D1&usg=3DAFQjCNGKZpmhU-Tg4DfHRpyK62T2WNNZKw>
>>
>>
>>
>> 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=20
>>>> removes a large class of such surprises is that you should never use=
=20
>>>> implicits of primitive types or common collections thereof=E2=80=94the=
risk for=20
>>>> having these picked up in the wrong context is just too high. In this =
case,=20
>>>> since Map extends Function1 your implicit value is not innocent at all=
,=20
>>>> because it presents another way how to turn a String into something th=
at=20
>>>> has a 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 da=
ngerous=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 explicit=
ly=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=20
>>>> Groups "scala-language" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send=
=20
>>>> 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.
------=_Part_979_859259966.1478328329065
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><br>I think there is an old discussion about why there'=
;s no warning on the val? But I bet a third party lint warning is warranted=
.<br><br>Using def with parens helps:<br><br>implicit def innocentMap() =3D=
Map("foo" -> Set("bar"))<br><br>If I were a rapper,=
I'd be def with parens. Something about living with my parents.<br><br=
>On Thursday, October 27, 2016 at 7:47:14 AM UTC-7, Jasper-M wrote:<blockqu=
ote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left=
: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr">Hi Andrew,<div><br></=
div><div>It's probably best if a warning is given for all implicit conv=
ersions (as I thought was supposed to be the case now, unless you import sc=
ala.language.<wbr>implicitConversions).</div><div>But I think it's high=
ly unlikely that an implicit val or def that has a type (explicit or inferr=
ed) that is not Function1 but a subtype thereof (like List[A], Map[A,B]) is=
intended to be an implicit conversion.</div><div><br></div><div>So I would=
define an "obvious implicit conversion" as any implicit whose st=
atic type T =3D:=3D Function1[A,B] or can be eta expanded to a value with s=
uch a type T.</div><div><br></div><div><br></div><div>Kinds regards,</div><=
div>Jasper<br><br>Op donderdag 27 oktober 2016 14:52:25 UTC+2 schreef Andre=
w Phillips:<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">Hi Jasp=
er<div><br></div><div><i>> This is a case where a warning is actually ne=
cessary because it is not obvious that you're introducing an implicit c=
onversion</i></div><div><br></div><div>Would you define "obvious intro=
ductions" as being "implicit defs only", or would you includ=
e other options too?</div><div><br></div><div>Since Scala generally treats =
<font face=3D"courier new, monospace">defs</font> and values of type <font =
face=3D"courier new, monospace">Function1</font> interchangeably, it seems =
consistent to me to also regard any implicit value of type <font face=3D"co=
urier new, monospace">Function1</font> as being an "intended" imp=
licit conversion. Or rather, the idea of potentially introducing a distinct=
ion between <font face=3D"courier new, monospace">def</font>s and <font fac=
e=3D"courier new, monospace">Function1</font> values for implicits only see=
ms inconsistent.</div><div><br></div><div>Should a possible warning in this=
case only warn on <font face=3D"courier new, monospace">Function1</font> s=
ubtypes, or do you have some other way of marking implicits as "intent=
ional" in mind?</div><div><br></div><div>Regards</div><div><br></div><=
div>ap</div><div><br></div><div>PS: Another occurrence of this:=C2=A0<a hre=
f=3D"http://www.google.com/url?q=3Dhttp%3A%2F%2Fscalapuzzlers.com%2F%23pzzl=
r-054&sa=3DD&sntz=3D1&usg=3DAFQjCNGKZpmhU-Tg4DfHRpyK62T2WNNZKw"=
rel=3D"nofollow" target=3D"_blank" onmousedown=3D"this.href=3D'http://=
www.google.com/url?q\x3dhttp%3A%2F%2Fscalapuzzlers.com%2F%23pzzlr-054\x26sa=
\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGKZpmhU-Tg4DfHRpyK62T2WNNZKw';return=
true;" onclick=3D"this.href=3D'http://www.google.com/url?q\x3dhttp%3A%=
2F%2Fscalapuzzlers.com%2F%23pzzlr-054\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQ=
jCNGKZpmhU-Tg4DfHRpyK62T2WNNZKw';return true;">http://scalapuzzlers.<wb=
r>com/#pzzlr-054</a></div><div><br></div><div><br></div><div><br>On Thursda=
y, 27 October 2016 03:02:06 UTC-4, Jasper-M wrote:<blockquote class=3D"gma=
il_quote" style=3D"margin:0;margin-left:0.8ex;border-left:1px #ccc solid;pa=
dding-left:1ex"><div dir=3D"ltr"><br><br>Op donderdag 27 oktober 2016 02:27=
:53 UTC+2 schreef 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-wrap: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 is that you should never use implicits of primitive types or com=
mon collections thereof=E2=80=94the risk for having these picked up in the =
wrong context is just too high. In this case, since Map extends Function1 y=
our 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><div>One thing that might be considered a bug is that declaring th=
is value does not raise an implicit conversion warning=E2=80=94normally suc=
h dangerous activities are protected by a language feature import.</div><di=
v><br></div></div></blockquote><div><br></div><div>I definitely consider th=
at a bug. This is a case where a warning is actually necessary because it i=
s not obvious that you're introducing an implicit conversion, but you d=
on't get a warning. While if you explicitly write an implicit conversio=
n you do get one.</div><div>=C2=A0</div><blockquote class=3D"gmail_quote" s=
tyle=3D"margin:0;margin-left:0.8ex;border-left:1px #ccc solid;padding-left:=
1ex"><div 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. 2016 kl. 14:53 skrev Gallus Anonimus <<a rel=3D"nofollow">drzo...@=
tlen.pl</a>>:</div><br><div><div dir=3D"ltr">Hi,<br>In following code:<b=
r><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"col=
or:#008">object</span><span> </span><span style=3D"color:#606">TheApp</span=
><span> </span><span style=3D"color:#660">{</span><span><br>=C2=A0 <br>=C2=
=A0 </span><span style=3D"color:#008">def</span><span> main</span><span sty=
le=3D"color:#660">(</span><span>args </span><span style=3D"color:#660">:</s=
pan><span> </span><span style=3D"color:#606">Array</span><span style=3D"col=
or:#660">[</span><span style=3D"color:#606">String</span><span style=3D"col=
or:#660">])</span><span> </span><span style=3D"color:#660">{</span><span><b=
r>=C2=A0 =C2=A0 </span><span style=3D"color:#008">implicit</span><span> val=
innocentMap </span><span style=3D"color:#660">=3D</span><span> </span><spa=
n style=3D"color:#606">Map</span><span style=3D"color:#660">(</span><span s=
tyle=3D"color:#080">"foo"</span><span> </span><span style=3D"colo=
r:#660">-></span><span> </span><span style=3D"color:#606">Set</span><spa=
n style=3D"color:#660">(</span><span style=3D"color:#080">"bar"</=
span><span style=3D"color:#660">))</span><span><br>=C2=A0 =C2=A0 </span><sp=
an style=3D"color:#080">"baz"</span><span style=3D"color:#660">.<=
/span><span>nonEmpty<br>=C2=A0 </span><span style=3D"color:#660">}</span><s=
pan><br>=C2=A0 <br></span><span style=3D"color:#660">}</span><span><br></sp=
an></div></code></div>instead of computing expected true as "baz"=
string is non empty<br>results in "java.util.<wbr>NoSuchElementExcept=
ion: key not found: baz"<code><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 "baz".nonEmpty looks like this:<br=
><div style=3D"background-color:rgb(250,250,250);border-color:rgb(187,187,1=
87);border-style:solid;border-width:1px"><code><div><span style=3D"color:#6=
60">((</span><span style=3D"color:#606">TraversableOnce</span><span style=
=3D"color:#660">)</span><span>innocentMap</span><span style=3D"color:#660">=
.</span><span><wbr>apply</span><span style=3D"color:#660">(</span><span sty=
le=3D"color:#080">"baz"</span><span style=3D"color:#660">)).</spa=
n><span>nonEmpty</span><span style=3D"color:#660">();</span></div></code></=
div><br>Can anyone tell me why innocentMap.apply is magically called here?<=
br>Highly unexpected behaviour. If innocentMap has different type like:<br>=
<div style=3D"background-color:rgb(250,250,250);border-color:rgb(187,187,18=
7);border-style:solid;border-width:1px"><code><div><span style=3D"color:#00=
8">implicit</span><span> val innocentMap </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"color:#080">"foo"</span><span=
> </span><span style=3D"color:#660">-></span><span> </span><span style=
=3D"color:#660">(</span><span style=3D"color:#080">"bar"</span><s=
pan style=3D"color:#660">))</span></div></code></div>code behaves as expect=
ed.<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></blockquote></div></div></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 />
------=_Part_979_859259966.1478328329065--
------=_Part_978_817057887.1478328329065--