<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>BlackBox : Overriding the 'as' operator</title>
    <link>http://www.warneronstine.com/blog/articles/2007/12/06/overriding-the-as-operator</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Where technology and art disappear</description>
    <item>
      <title>Overriding the 'as' operator</title>
      <description>&lt;p&gt;A friend of mine sent me a link to Charles Nutter&amp;#8217;s blog on &lt;a href="http://headius.blogspot.com/2007/12/groovy-in-ruby-implement-interface-with.html"&gt;implementing interfaces using the &lt;code&gt;as&lt;/code&gt; operator&lt;/a&gt;. While I was explaining how it worked to him I remembered that like many operators you can also override this one using &lt;code&gt;asType()&lt;/code&gt;. This got us to thinking when would you use this functionality?&lt;/p&gt;

&lt;p&gt;I have one project I&amp;#8217;m working on right now (not ready to talk yet about as I want more code in place before I unveil it, :-P) but this might be useful. Say that you wanted to duck-type your object for specific class instances. Overriding &lt;code&gt;as&lt;/code&gt; may be one place to do it. &lt;/p&gt;

&lt;p&gt;When would this make sense? Personally I think something like this makes the most sense in a library that provides some kind of functionality to a bunch of classes (rather than a one-off case). But I&amp;#8217;m curious to see how others have used this functionality, would help to shed some light on other uses I hadn&amp;#8217;t thought of. &lt;/p&gt;</description>
      <pubDate>Thu, 06 Dec 2007 09:40:25 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:4e93957b-7b7c-4893-b1e7-a9f5c6d13d98</guid>
      <author>Warner Onstine</author>
      <link>http://www.warneronstine.com/blog/articles/2007/12/06/overriding-the-as-operator</link>
      <category>programming</category>
      <category>dsl</category>
      <category>groovy</category>
      <trackback:ping>http://www.warneronstine.com/blog/articles/trackback/307</trackback:ping>
    </item>
    <item>
      <title>"Overriding the 'as' operator" by Warner Onstine</title>
      <description>&lt;p&gt;Very cool, just did a quick search on &lt;a href="http://www.krugle.org/kse/files?query=astype&amp;amp;lang=groovy" rel="nofollow"&gt;Krugle&lt;/a&gt; (yours is the only one that showed up).&lt;/p&gt;

&lt;p&gt;So, what you're doing is creating three metaclasses for ArrayList, HashMap and String and then overriding the &lt;code&gt;asType&lt;/code&gt; call through &lt;code&gt;invokeMethod&lt;/code&gt;. Pretty slick.&lt;/p&gt;</description>
      <pubDate>Fri, 07 Dec 2007 15:28:26 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:23fefed8-1ba6-4104-91c4-81e899f12610</guid>
      <link>http://www.warneronstine.com/blog/articles/2007/12/06/overriding-the-as-operator#comment-61</link>
    </item>
    <item>
      <title>"Overriding the 'as' operator" by Andres Almiray</title>
      <description>&lt;p&gt;Well for my part I use it on Json-lib to transform Strings, Lists and Maps to their JSON representation with a single coercion with as&lt;/p&gt;

&lt;p&gt;&lt;a href="http://json-lib.sourceforge.net/groovy.html" rel="nofollow"&gt;http://json-lib.sourceforge.net/groovy.html&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Fri, 07 Dec 2007 00:29:32 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:2fdbeda8-f4eb-44d5-8f7a-4f3e12d43b4d</guid>
      <link>http://www.warneronstine.com/blog/articles/2007/12/06/overriding-the-as-operator#comment-60</link>
    </item>
  </channel>
</rss>
