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

Jasper-M <[email protected]> Thu, 27 Oct 2016 07:47:13 -0700 (PDT)
Newsgroups gmane.comp.lang.scala
Message-ID <[email protected]>
------=_Part_217_1066624821.1477579633995
Content-Type: multipart/alternative; 
	boundary="----=_Part_218_1157207835.1477579633995"

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

Hi Andrew,

It's probably best if a warning is given for all implicit conversions (as I=
=20
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 with=
=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 not=
=20
> obvious that you're introducing an implicit conversion*
>
> Would you define "obvious introductions" as being "implicit defs only", o=
r=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=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 mi=
nd?
>
> 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 remove=
s=20
>>> a 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 havi=
ng these=20
>>> picked up in the wrong context is just too high. In this case, since Ma=
p=20
>>> extends Function1 your implicit value is not innocent at all, because i=
t=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 dan=
gerous=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 explicitl=
y=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_218_1157207835.1477579633995
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Hi Andrew,<div><br></div><div>It&#39;s probably best if a =
warning is given for all implicit conversions (as I thought was supposed to=
 be the case now, unless you import scala.language.implicitConversions).</d=
iv><div>But I think it&#39;s highly unlikely that an implicit val or def th=
at has a type (explicit or inferred) that is not Function1 but a subtype th=
ereof (like List[A], Map[A,B]) is intended to be an implicit conversion.</d=
iv><div><br></div><div>So I would define an &quot;obvious implicit conversi=
on&quot; as any implicit whose static type T =3D:=3D Function1[A,B] or can =
be eta expanded to a value with such 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 Andrew Phillips:<blockquote class=3D"gmail_quot=
e" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;paddin=
g-left: 1ex;"><div dir=3D"ltr">Hi Jasper<div><br></div><div><i>&gt; This is=
 a case where a warning is actually necessary because it is not obvious tha=
t you&#39;re introducing an implicit conversion</i></div><div><br></div><di=
v>Would you define &quot;obvious introductions&quot; as being &quot;implici=
t defs only&quot;, or would you include 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">Funct=
ion1</font> interchangeably, it seems consistent to me to also regard any i=
mplicit value of type <font face=3D"courier new, monospace">Function1</font=
> as being an &quot;intended&quot; implicit conversion. Or rather, the idea=
 of potentially introducing a distinction between <font face=3D"courier new=
, monospace">def</font>s and <font face=3D"courier new, monospace">Function=
1</font> values for implicits only seems inconsistent.</div><div><br></div>=
<div>Should a possible warning in this case only warn on <font face=3D"cour=
ier new, monospace">Function1</font> subtypes, or do you have some other wa=
y of marking implicits as &quot;intentional&quot; in mind?</div><div><br></=
div><div>Regards</div><div><br></div><div>ap</div><div><br></div><div>PS: A=
nother occurrence of this:=C2=A0<a href=3D"http://www.google.com/url?q=3Dht=
tp%3A%2F%2Fscalapuzzlers.com%2F%23pzzlr-054&amp;sa=3DD&amp;sntz=3D1&amp;usg=
=3DAFQjCNGKZpmhU-Tg4DfHRpyK62T2WNNZKw" target=3D"_blank" rel=3D"nofollow" o=
nmousedown=3D"this.href=3D&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2F=
scalapuzzlers.com%2F%23pzzlr-054\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGK=
ZpmhU-Tg4DfHRpyK62T2WNNZKw&#39;;return true;" onclick=3D"this.href=3D&#39;h=
ttp://www.google.com/url?q\x3dhttp%3A%2F%2Fscalapuzzlers.com%2F%23pzzlr-054=
\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGKZpmhU-Tg4DfHRpyK62T2WNNZKw&#39;;=
return true;">http://scalapuzzlers.<wbr>com/#pzzlr-054</a></div><div><br></=
div><div><br></div><div><br>On Thursday, 27 October 2016 03:02:06 UTC-4, Ja=
sper-M  wrote:<blockquote class=3D"gmail_quote" style=3D"margin:0;margin-le=
ft: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&#39;re introduc=
ing an implicit conversion, but you don&#39;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 &lt;<a rel=3D"nofollow">[email protected]</a>&gt;:</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">&quot;foo&quot;</sp=
an><span> </span><span style=3D"color:#660">-&gt;</span><span> </span><span=
 style=3D"color:#606">Set</span><span style=3D"color:#660">(</span><span st=
yle=3D"color:#080">&quot;bar&quot;</span><span style=3D"color:#660">))</spa=
n><span><br>=C2=A0 =C2=A0 </span><span style=3D"color:#080">&quot;baz&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 of comp=
uting expected true as &quot;baz&quot; string is non empty<br>results in &q=
uot;java.util.<wbr>NoSuchElementException: key not found: baz&quot;<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 &quot;=
baz&quot;.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">&quot;baz&quot;</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">&quot;foo&quot;</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">&quot;bar&quot;</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&quot; 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&#39;https://gro=
ups.google.com/d/optout&#39;;return true;" onclick=3D"this.href=3D&#39;http=
s://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></blockquote></=
div></div></blockquote></div></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_218_1157207835.1477579633995--

------=_Part_217_1066624821.1477579633995--