<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Implicits for the Masses</title>
	<atom:link href="http://www.deskchecked.com/2008/07/05/implicits-for-the-masses/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.deskchecked.com/2008/07/05/implicits-for-the-masses/</link>
	<description>Thomas Lee's programming blog</description>
	<lastBuildDate>Sun, 06 Dec 2009 11:37:11 +1100</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Tony Morris</title>
		<link>http://www.deskchecked.com/2008/07/05/implicits-for-the-masses/comment-page-1/#comment-1360</link>
		<dc:creator>Tony Morris</dc:creator>
		<pubDate>Sat, 12 Jul 2008 08:51:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.vector-seven.com/?p=72#comment-1360</guid>
		<description>Seth got it right. Notice that an objector often resorts to introducing side-effects (inadvertently?) in order to undermine high-level abstractions. I glossed over this common occurrence in the referenced article. It&#039;s a tough one to address and is often very confrontational (and even traumatic for some!), especially over an internet forum.

Some people&#039;s brains just won&#039;t bend that way; they have far too many defences against it. Cognitive dissonance (doublethink) is the barrier.</description>
		<content:encoded><![CDATA[<p>Seth got it right. Notice that an objector often resorts to introducing side-effects (inadvertently?) in order to undermine high-level abstractions. I glossed over this common occurrence in the referenced article. It&#8217;s a tough one to address and is often very confrontational (and even traumatic for some!), especially over an internet forum.</p>
<p>Some people&#8217;s brains just won&#8217;t bend that way; they have far too many defences against it. Cognitive dissonance (doublethink) is the barrier.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Seth Tisue</title>
		<link>http://www.deskchecked.com/2008/07/05/implicits-for-the-masses/comment-page-1/#comment-1348</link>
		<dc:creator>Seth Tisue</dc:creator>
		<pubDate>Tue, 08 Jul 2008 18:25:45 +0000</pubDate>
		<guid isPermaLink="false">http://www.vector-seven.com/?p=72#comment-1348</guid>
		<description>Sean, I agree that the Java code you posted is &quot;universally acknowledged as being bad code&quot;. But if you change hello to be declared final, then I think it&#039;s no longer bad code at all! Strings are immutable, the variable is final.... no problem.

I think it&#039;s fine for the behavior of code to depend on a value which is defined in an enclosing scope, in fact, it&#039;s good. It makes that code configurable, which you sometimes want and need. You only run into trouble if the value is mutable, or if the variable it&#039;s stored in is assignable (that is, non-final).</description>
		<content:encoded><![CDATA[<p>Sean, I agree that the Java code you posted is &#8220;universally acknowledged as being bad code&#8221;. But if you change hello to be declared final, then I think it&#8217;s no longer bad code at all! Strings are immutable, the variable is final&#8230;. no problem.</p>
<p>I think it&#8217;s fine for the behavior of code to depend on a value which is defined in an enclosing scope, in fact, it&#8217;s good. It makes that code configurable, which you sometimes want and need. You only run into trouble if the value is mutable, or if the variable it&#8217;s stored in is assignable (that is, non-final).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tom</title>
		<link>http://www.deskchecked.com/2008/07/05/implicits-for-the-masses/comment-page-1/#comment-1347</link>
		<dc:creator>tom</dc:creator>
		<pubDate>Tue, 08 Jul 2008 10:23:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.vector-seven.com/?p=72#comment-1347</guid>
		<description>German,

Theoretically speaking, that&#039;s not necessarily the case. Remember that the language is statically typed, so a fair bit can actually be inferred by the compiler. The fact a wrapper object will be required for two separate calls can be determined at compile time, leaving the compiler to determine whether it should generate code to make calls to &quot;frame&quot; or to its implicit counterpart.

Whether or not this is true in practice is another question entirely, but it really wouldn&#039;t surprise me if that were the case for this trivial example.</description>
		<content:encoded><![CDATA[<p>German,</p>
<p>Theoretically speaking, that&#8217;s not necessarily the case. Remember that the language is statically typed, so a fair bit can actually be inferred by the compiler. The fact a wrapper object will be required for two separate calls can be determined at compile time, leaving the compiler to determine whether it should generate code to make calls to &#8220;frame&#8221; or to its implicit counterpart.</p>
<p>Whether or not this is true in practice is another question entirely, but it really wouldn&#8217;t surprise me if that were the case for this trivial example.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: German</title>
		<link>http://www.deskchecked.com/2008/07/05/implicits-for-the-masses/comment-page-1/#comment-1342</link>
		<dc:creator>German</dc:creator>
		<pubDate>Mon, 07 Jul 2008 19:34:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.vector-seven.com/?p=72#comment-1342</guid>
		<description>I like implicits. For me, what feels uneasy about it is that, in a snippet like this...

val frame = new JFrame
frame.title = &quot;My Application&quot;
frame.visible = true

...a wrapper is being created and discarded twice at runtime. Or at least so I assume - am I right? is this considered a problem?</description>
		<content:encoded><![CDATA[<p>I like implicits. For me, what feels uneasy about it is that, in a snippet like this&#8230;</p>
<p>val frame = new JFrame<br />
frame.title = &#8220;My Application&#8221;<br />
frame.visible = true</p>
<p>&#8230;a wrapper is being created and discarded twice at runtime. Or at least so I assume &#8211; am I right? is this considered a problem?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Thomas Kappler</title>
		<link>http://www.deskchecked.com/2008/07/05/implicits-for-the-masses/comment-page-1/#comment-1335</link>
		<dc:creator>Thomas Kappler</dc:creator>
		<pubDate>Sat, 05 Jul 2008 20:45:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.vector-seven.com/?p=72#comment-1335</guid>
		<description>Excellent explanation, Daniel. I&#039;m one of those guys following the Scala world, but not having done anything with it yet. Several articles on implicits left me a bit puzzled, but this comment was very insightful. Consider turning it into a blog article.

-- Thomas</description>
		<content:encoded><![CDATA[<p>Excellent explanation, Daniel. I&#8217;m one of those guys following the Scala world, but not having done anything with it yet. Several articles on implicits left me a bit puzzled, but this comment was very insightful. Consider turning it into a blog article.</p>
<p>&#8211; Thomas</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Debasish Ghosh</title>
		<link>http://www.deskchecked.com/2008/07/05/implicits-for-the-masses/comment-page-1/#comment-1332</link>
		<dc:creator>Debasish Ghosh</dc:creator>
		<pubDate>Sat, 05 Jul 2008 16:48:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.vector-seven.com/?p=72#comment-1332</guid>
		<description>Sean -

Implicits in Scala are lexically scoped, unlike globally open classes in Ruby. I blogged about it sometime back. http://debasishg.blogspot.com/2008/02/why-i-like-scalas-lexically-scoped-open.html .. If this is of any help to you .. 

Cheers.
- Debasish</description>
		<content:encoded><![CDATA[<p>Sean -</p>
<p>Implicits in Scala are lexically scoped, unlike globally open classes in Ruby. I blogged about it sometime back. <a href="http://debasishg.blogspot.com/2008/02/why-i-like-scalas-lexically-scoped-open.html" rel="nofollow">http://debasishg.blogspot.com/2008/02/why-i-like-scalas-lexically-scoped-open.html</a> .. If this is of any help to you .. </p>
<p>Cheers.<br />
- Debasish</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tom</title>
		<link>http://www.deskchecked.com/2008/07/05/implicits-for-the-masses/comment-page-1/#comment-1331</link>
		<dc:creator>tom</dc:creator>
		<pubDate>Sat, 05 Jul 2008 16:11:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.vector-seven.com/?p=72#comment-1331</guid>
		<description>&lt;em&gt;/me looks on in disbelief&lt;/em&gt;

Wow, well that was informative. Thanks, Daniel!</description>
		<content:encoded><![CDATA[<p><em>/me looks on in disbelief</em></p>
<p>Wow, well that was informative. Thanks, Daniel!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Spiewak</title>
		<link>http://www.deskchecked.com/2008/07/05/implicits-for-the-masses/comment-page-1/#comment-1330</link>
		<dc:creator>Daniel Spiewak</dc:creator>
		<pubDate>Sat, 05 Jul 2008 15:56:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.vector-seven.com/?p=72#comment-1330</guid>
		<description>I realize I&#039;m coming a bit late to this conversation.  (curse you, timezone offsets!)  Let&#039;s see if I can catch up...

As already pointed out, the primary use of implicit *anything* in Scala is for type coercion.  The reason for this as a separate language construct is to avoid the random and almost uncontrolled coercion that we have in Java and C (C   has a bit more control, but it&#039;s still difficult to deal with).

So anyway, the idea is that you mark a function as `implicit` and it will be automatically inserted as needed (I realize this has already been addressed, I&#039;m just trying to lay the groundwork for my next point):

implicit def str2int(str: String) = str.toInt
val one: Int = &quot;1&quot;

The second line becomes:

val one: Int = str2int(&quot;1&quot;)

The unfortunate truth is that the addition of this language construct *demands* the introduction of the implicit parameters, otherwise it is no where near as useful.  Remember that implicit functions are lexically scoped (just like everything else in Scala).  This means that a type conversion can exist in one scope, but on in the one you really need it.  We can imagine this as a problem in the following sort of scenario:

def doSomething(value: Option[Int]) = value match {
  case Some(i) =&gt; i
  case None =&gt; 0
}

All this does is unbind the Intfrom the Option.  Now, we have already defined an implicit conversion from String to Int, but even with this conversion, we cannot do the following:

val param = Some(&quot;123&quot;)
doSomething(param)      // ERROR!

The reason is that the conversion must be applied *inside* the doSomething, and the conversion is simply not in scope within that method (which is presumably in some other class).  In fact, allowing this method to see the conversion as being in scope would break encapsulation horribly and really be some cause for concern.  The solution is to use an implicit parameter:

def doSomething[T](value: Option[T])(implicit def conv2int(param: T): Int): Int =
  value match {
    case Some(i) =&gt; i
    case None = 0
  }

Now we can pass to this method an Option of *any* type which has some conversion to Int.  This is far more flexible than our previous version, and just as type-safe.  We have avoided breaking the scoping laws, and we still managed to make the implicit conversion available to the doSomething method.  Oh, and btw, by currying the function in that way (the double-parens notation), we have ensured that the method signature has not been changed by requiring this additional parameter.  As a matter of interest, we could express this using an equivalent (but more concise) syntax (note, I haven&#039;t checked this and I don&#039;t often use this syntax, so I might have gotten it slightly wrong):

def doSomething[Int &lt;% T](value: Option[T]): Int

The issue you are raising has to do with implicit parameters as variables (using that term loosely, since idiomatic Scala &quot;variables&quot; are actually constants).  e.g. the following:

implicit val name = &quot;Daniel&quot;
def doSomething(implicit val name: String) = name   &quot; Spiewak&quot;

doSomething   // implicitly grabs parameter

I agree this is ugly and nasty.  It&#039;s nothing like global variables (since everything is still scoped), but it *is* bad design on the developer&#039;s part.  The truth is that this is a misuse of the language feature.  It is *allowed* because variables are actually methods in disguise, and to keep the language consistent, the implicit modifier must be applicable to any method, even if it&#039;s a variable.  There are still times when this is desirable (as was pointed out, Scala&#039;s documentation has a nice example), but in the general case, I think this should be avoided.

The good news: it is avoided!  I don&#039;t know of anyone who structures their code like this.  Yes, there are implicit params from time to time, but always to satisfy my first example (passing the implicit conversion implicitly) and not to save a little typing with the variables.

Does this make a bit more sense?</description>
		<content:encoded><![CDATA[<p>I realize I&#8217;m coming a bit late to this conversation.  (curse you, timezone offsets!)  Let&#8217;s see if I can catch up&#8230;</p>
<p>As already pointed out, the primary use of implicit *anything* in Scala is for type coercion.  The reason for this as a separate language construct is to avoid the random and almost uncontrolled coercion that we have in Java and C (C   has a bit more control, but it&#8217;s still difficult to deal with).</p>
<p>So anyway, the idea is that you mark a function as `implicit` and it will be automatically inserted as needed (I realize this has already been addressed, I&#8217;m just trying to lay the groundwork for my next point):</p>
<p>implicit def str2int(str: String) = str.toInt<br />
val one: Int = &#8220;1&#8243;</p>
<p>The second line becomes:</p>
<p>val one: Int = str2int(&#8221;1&#8243;)</p>
<p>The unfortunate truth is that the addition of this language construct *demands* the introduction of the implicit parameters, otherwise it is no where near as useful.  Remember that implicit functions are lexically scoped (just like everything else in Scala).  This means that a type conversion can exist in one scope, but on in the one you really need it.  We can imagine this as a problem in the following sort of scenario:</p>
<p>def doSomething(value: Option[Int]) = value match {<br />
  case Some(i) =&gt; i<br />
  case None =&gt; 0<br />
}</p>
<p>All this does is unbind the Intfrom the Option.  Now, we have already defined an implicit conversion from String to Int, but even with this conversion, we cannot do the following:</p>
<p>val param = Some(&#8221;123&#8243;)<br />
doSomething(param)      // ERROR!</p>
<p>The reason is that the conversion must be applied *inside* the doSomething, and the conversion is simply not in scope within that method (which is presumably in some other class).  In fact, allowing this method to see the conversion as being in scope would break encapsulation horribly and really be some cause for concern.  The solution is to use an implicit parameter:</p>
<p>def doSomething[T](value: Option[T])(implicit def conv2int(param: T): Int): Int =<br />
  value match {<br />
    case Some(i) =&gt; i<br />
    case None = 0<br />
  }</p>
<p>Now we can pass to this method an Option of *any* type which has some conversion to Int.  This is far more flexible than our previous version, and just as type-safe.  We have avoided breaking the scoping laws, and we still managed to make the implicit conversion available to the doSomething method.  Oh, and btw, by currying the function in that way (the double-parens notation), we have ensured that the method signature has not been changed by requiring this additional parameter.  As a matter of interest, we could express this using an equivalent (but more concise) syntax (note, I haven&#8217;t checked this and I don&#8217;t often use this syntax, so I might have gotten it slightly wrong):</p>
<p>def doSomething[Int &lt;% T](value: Option[T]): Int</p>
<p>The issue you are raising has to do with implicit parameters as variables (using that term loosely, since idiomatic Scala &#8220;variables&#8221; are actually constants).  e.g. the following:</p>
<p>implicit val name = &#8220;Daniel&#8221;<br />
def doSomething(implicit val name: String) = name   &#8221; Spiewak&#8221;</p>
<p>doSomething   // implicitly grabs parameter</p>
<p>I agree this is ugly and nasty.  It&#8217;s nothing like global variables (since everything is still scoped), but it *is* bad design on the developer&#8217;s part.  The truth is that this is a misuse of the language feature.  It is *allowed* because variables are actually methods in disguise, and to keep the language consistent, the implicit modifier must be applicable to any method, even if it&#8217;s a variable.  There are still times when this is desirable (as was pointed out, Scala&#8217;s documentation has a nice example), but in the general case, I think this should be avoided.</p>
<p>The good news: it is avoided!  I don&#8217;t know of anyone who structures their code like this.  Yes, there are implicit params from time to time, but always to satisfy my first example (passing the implicit conversion implicitly) and not to save a little typing with the variables.</p>
<p>Does this make a bit more sense?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tom</title>
		<link>http://www.deskchecked.com/2008/07/05/implicits-for-the-masses/comment-page-1/#comment-1329</link>
		<dc:creator>tom</dc:creator>
		<pubDate>Sat, 05 Jul 2008 15:51:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.vector-seven.com/?p=72#comment-1329</guid>
		<description>Sean: I understand the uneasiness. I&#039;d bet if such a feature were used without due diligence, you&#039;d wind up with a big mess on your hands.

Having said that, rather than just calling the feature awful for some arbitrary example of how it might be used for evil, I&#039;d be open to using implicit parameters if it made my code more readable. I certainly wouldn&#039;t put them in the same boat as globals, not even from a pure perspective -- that a value is implicitly passed to a function doesn&#039;t suddenly make it mutable nor does its use imply side effects.

&lt;blockquote&gt;I just don’t feel comfortable using a method whose behavior will change silently depending on what mixin I use.&lt;/blockquote&gt;

So polymorphism is right out the window then? Boy, you&#039;re a hard one to please ;)

In all seriousness, I&#039;m sure you could use implicit values in a way that&#039;s completely confusing and baffling. My advice is to try not to. :) Anyway, the fact a feature exists doesn&#039;t necessitate its use. Use language features where they make sense.

Oh, and I&#039;m not a functional die-hard -- just a general language geek. Honest. :)</description>
		<content:encoded><![CDATA[<p>Sean: I understand the uneasiness. I&#8217;d bet if such a feature were used without due diligence, you&#8217;d wind up with a big mess on your hands.</p>
<p>Having said that, rather than just calling the feature awful for some arbitrary example of how it might be used for evil, I&#8217;d be open to using implicit parameters if it made my code more readable. I certainly wouldn&#8217;t put them in the same boat as globals, not even from a pure perspective &#8212; that a value is implicitly passed to a function doesn&#8217;t suddenly make it mutable nor does its use imply side effects.</p>
<blockquote><p>I just don’t feel comfortable using a method whose behavior will change silently depending on what mixin I use.</p></blockquote>
<p>So polymorphism is right out the window then? Boy, you&#8217;re a hard one to please <img src='http://www.deskchecked.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>In all seriousness, I&#8217;m sure you could use implicit values in a way that&#8217;s completely confusing and baffling. My advice is to try not to. <img src='http://www.deskchecked.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Anyway, the fact a feature exists doesn&#8217;t necessitate its use. Use language features where they make sense.</p>
<p>Oh, and I&#8217;m not a functional die-hard &#8212; just a general language geek. Honest. <img src='http://www.deskchecked.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sean Cobb</title>
		<link>http://www.deskchecked.com/2008/07/05/implicits-for-the-masses/comment-page-1/#comment-1328</link>
		<dc:creator>Sean Cobb</dc:creator>
		<pubDate>Sat, 05 Jul 2008 14:46:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.vector-seven.com/?p=72#comment-1328</guid>
		<description>Tom,

The scope of the implicit doesn&#039;t matter much, the important part is that it&#039;s outside the method that&#039;s using it, and that&#039;s what is really bothering me.  

I would think this should be even more of a concern for functional programmers like yourself and Tony, but you seem to be giving a puzzling free pass to Scala on that respect.

I just don&#039;t feel comfortable using a method whose behavior will change silently depending on what mixin I use.</description>
		<content:encoded><![CDATA[<p>Tom,</p>
<p>The scope of the implicit doesn&#8217;t matter much, the important part is that it&#8217;s outside the method that&#8217;s using it, and that&#8217;s what is really bothering me.  </p>
<p>I would think this should be even more of a concern for functional programmers like yourself and Tony, but you seem to be giving a puzzling free pass to Scala on that respect.</p>
<p>I just don&#8217;t feel comfortable using a method whose behavior will change silently depending on what mixin I use.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
