<?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: Generic variable to the current type</title>
	<atom:link href="http://brian.pontarelli.com/2009/03/06/generic-variable-to-the-current-type/feed/" rel="self" type="application/rss+xml" />
	<link>http://brian.pontarelli.com/2009/03/06/generic-variable-to-the-current-type/</link>
	<description>Brian Pontarelli</description>
	<lastBuildDate>Wed, 08 Feb 2012 15:34:20 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
	<item>
		<title>By: Brian Pontarelli</title>
		<link>http://brian.pontarelli.com/2009/03/06/generic-variable-to-the-current-type/#comment-68793</link>
		<dc:creator>Brian Pontarelli</dc:creator>
		<pubDate>Mon, 09 Mar 2009 20:28:32 +0000</pubDate>
		<guid isPermaLink="false">http://brian.pontarelli.com/?p=275#comment-68793</guid>
		<description>Hehe. Looks like someone has proposed this for inclusion in Java 7. Not sure if it will make it or not:

http://mail.openjdk.java.net/pipermail/coin-dev/2009-March/000107.html</description>
		<content:encoded><![CDATA[<p>Hehe. Looks like someone has proposed this for inclusion in Java 7. Not sure if it will make it or not:</p>
<p><a href="http://mail.openjdk.java.net/pipermail/coin-dev/2009-March/000107.html" rel="nofollow">http://mail.openjdk.java.net/pipermail/coin-dev/2009-March/000107.html</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Pontarelli</title>
		<link>http://brian.pontarelli.com/2009/03/06/generic-variable-to-the-current-type/#comment-68791</link>
		<dc:creator>Brian Pontarelli</dc:creator>
		<pubDate>Mon, 09 Mar 2009 20:21:54 +0000</pubDate>
		<guid isPermaLink="false">http://brian.pontarelli.com/?p=275#comment-68791</guid>
		<description>See, they are identical in my case. The &#039;this&#039; keyword is a compiler indicator to use the type of the variable. If b is a BaseBuilder, the method&#039;s return type will be BaseBuilder. If b is a FooBuilder, it will be a FooBuilder. This is the exact same way the generic works.

&lt;pre&gt;
BaseBuilder[FooBuilder] b;
b.add().build();

FooBuilder b;
b.add().build();
&lt;/pre&gt;

These are the same. The type of B can _only_ be a FooBuilder or a sub-class for both. If you further cast out the known type, the compiler should fail in both cases:

&lt;pre&gt;
BaseBuilder[FooBuilder] b;
BaseBuilder b_raw = b
b.add().build(); // FAIL

FooBuilder b;
BaseBuilder b_raw = b;
b.add().build(); // FAIL
&lt;/pre&gt;

Using a keyword (I like &#039;this&#039;) does the same thing (from the compilers perspective) as adding the generic information. It just provides a simpler mechanism with less cruft.</description>
		<content:encoded><![CDATA[<p>See, they are identical in my case. The &#8216;this&#8217; keyword is a compiler indicator to use the type of the variable. If b is a BaseBuilder, the method&#8217;s return type will be BaseBuilder. If b is a FooBuilder, it will be a FooBuilder. This is the exact same way the generic works.</p>
<pre>
BaseBuilder[FooBuilder] b;
b.add().build();

FooBuilder b;
b.add().build();
</pre>
<p>These are the same. The type of B can _only_ be a FooBuilder or a sub-class for both. If you further cast out the known type, the compiler should fail in both cases:</p>
<pre>
BaseBuilder[FooBuilder] b;
BaseBuilder b_raw = b
b.add().build(); // FAIL

FooBuilder b;
BaseBuilder b_raw = b;
b.add().build(); // FAIL
</pre>
<p>Using a keyword (I like &#8216;this&#8217;) does the same thing (from the compilers perspective) as adding the generic information. It just provides a simpler mechanism with less cruft.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matt Hellige</title>
		<link>http://brian.pontarelli.com/2009/03/06/generic-variable-to-the-current-type/#comment-68771</link>
		<dc:creator>Matt Hellige</dc:creator>
		<pubDate>Mon, 09 Mar 2009 19:03:43 +0000</pubDate>
		<guid isPermaLink="false">http://brian.pontarelli.com/?p=275#comment-68771</guid>
		<description>It&#039;s tempting to introduce a special keyword like &quot;ME&quot; or &quot;this&quot; to mean something like &quot;the concrete type of the eventual subclass.&quot; But I think you&#039;ll run into problems if you actually try to implement a type-checker for this language. The problem comes when checking a method call like &quot;b.add().foo()&quot; when the known type of b is just BaseBuilder (i.e., not a concrete subclass). The problem is: what does add return? You know it must be a BaseBuilder, but the whole point of the &quot;this&quot; return type is that you really need to know, for example, that it&#039;s a FooBuilder: knowing that it&#039;s BaseBuilder isn&#039;t enough if foo() is only defined on FooBuilder.

This is the problem that the &quot;extra&quot; generic solves. If instead of BaseBuilder, you know that b is a BaseBuilder[FooBuilder], and instead of &quot;ME&quot; or &quot;this&quot; add returns a T (== FooBuilder, in this case), then the typechecker has the information that it needs to typecheck a call like &quot;b.add().foo()&quot;. So in some sense the generic isn&#039;t extra: it serves to make the eventual concrete subclass known to the typechecker.</description>
		<content:encoded><![CDATA[<p>It&#8217;s tempting to introduce a special keyword like &#8220;ME&#8221; or &#8220;this&#8221; to mean something like &#8220;the concrete type of the eventual subclass.&#8221; But I think you&#8217;ll run into problems if you actually try to implement a type-checker for this language. The problem comes when checking a method call like &#8220;b.add().foo()&#8221; when the known type of b is just BaseBuilder (i.e., not a concrete subclass). The problem is: what does add return? You know it must be a BaseBuilder, but the whole point of the &#8220;this&#8221; return type is that you really need to know, for example, that it&#8217;s a FooBuilder: knowing that it&#8217;s BaseBuilder isn&#8217;t enough if foo() is only defined on FooBuilder.</p>
<p>This is the problem that the &#8220;extra&#8221; generic solves. If instead of BaseBuilder, you know that b is a BaseBuilder[FooBuilder], and instead of &#8220;ME&#8221; or &#8220;this&#8221; add returns a T (== FooBuilder, in this case), then the typechecker has the information that it needs to typecheck a call like &#8220;b.add().foo()&#8221;. So in some sense the generic isn&#8217;t extra: it serves to make the eventual concrete subclass known to the typechecker.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Pontarelli</title>
		<link>http://brian.pontarelli.com/2009/03/06/generic-variable-to-the-current-type/#comment-67768</link>
		<dc:creator>Brian Pontarelli</dc:creator>
		<pubDate>Fri, 06 Mar 2009 20:48:55 +0000</pubDate>
		<guid isPermaLink="false">http://brian.pontarelli.com/?p=275#comment-67768</guid>
		<description>Yeah, that will work also. Either is fine. The compiler doesn&#039;t care that the generic variable isn&#039;t filled out in the definition. At least Java doesn&#039;t care since it erases everything anyways and allows raw instances for generically typed classes.

The idea isn&#039;t that it can&#039;t be done in any language, just that it sucks to have to hack the language like that. With co-variant return types, we should be capable of creating these types of methods without any generic information. Actually, you could simplify it like this:

&lt;pre&gt;
class BaseBuilder {
  public this add() {
  }
}

class FooBuilder extends BaseBuilder {
}
&lt;/pre&gt;

That&#039;s the cleanest way.</description>
		<content:encoded><![CDATA[<p>Yeah, that will work also. Either is fine. The compiler doesn&#8217;t care that the generic variable isn&#8217;t filled out in the definition. At least Java doesn&#8217;t care since it erases everything anyways and allows raw instances for generically typed classes.</p>
<p>The idea isn&#8217;t that it can&#8217;t be done in any language, just that it sucks to have to hack the language like that. With co-variant return types, we should be capable of creating these types of methods without any generic information. Actually, you could simplify it like this:</p>
<pre>
class BaseBuilder {
  public this add() {
  }
}

class FooBuilder extends BaseBuilder {
}
</pre>
<p>That&#8217;s the cleanest way.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matt Hellige</title>
		<link>http://brian.pontarelli.com/2009/03/06/generic-variable-to-the-current-type/#comment-67761</link>
		<dc:creator>Matt Hellige</dc:creator>
		<pubDate>Fri, 06 Mar 2009 20:12:32 +0000</pubDate>
		<guid isPermaLink="false">http://brian.pontarelli.com/?p=275#comment-67761</guid>
		<description>Stupid Wordpress ate my pre tags and my less-than sign... Trying again, the first line should be:

class BaseBuilder[T &lt;: BaseBuilder[T]] { this: T =&gt; 

Will that work?</description>
		<content:encoded><![CDATA[<p>Stupid WordPress ate my pre tags and my less-than sign&#8230; Trying again, the first line should be:</p>
<p>class BaseBuilder[T &lt;: BaseBuilder[T]] { this: T =&gt; </p>
<p>Will that work?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matt Hellige</title>
		<link>http://brian.pontarelli.com/2009/03/06/generic-variable-to-the-current-type/#comment-67760</link>
		<dc:creator>Matt Hellige</dc:creator>
		<pubDate>Fri, 06 Mar 2009 20:11:31 +0000</pubDate>
		<guid isPermaLink="false">http://brian.pontarelli.com/?p=275#comment-67760</guid>
		<description>Scala lets you improve this a bit with a self type annotation. I don&#039;t find the type parameter that ugly (after all, you need to get the actual type in there somehow), but the unchecked casts are nasty. Here&#039;s the pattern in Scala:


class BaseBuilder[T 
    def add(): T = {
        //...
        this
    }
    def divide(): T = {
        //...
        this
    }
    def multiply(): T = {
        //...
        this
    }
}

class FooBuilder extends BaseBuilder[FooBuilder] {
}


The &quot;this: T =&gt;&quot; in the first line is the key. It says to treat &quot;this&quot; as having a static type of T rather than BaseBuilder. If anybody tries to subclass BaseBuilder without satisfying that requirement, it&#039;s a compile error. (You can get the same effect by just doing &quot;class BaseBuilder[T] { this: T =&gt; ... }&quot;, which just leaves off the bounds on T, but the error messages are uglier.) Anyway it basically allows T to act just like your ME type, and the compiler enforces it.

Incidentally, I&#039;d make a few other changes if I were doing it in Scala: I&#039;d make BaseBuilder a trait and clean up the syntax of the methods a bit. But basically that&#039;s the idea...</description>
		<content:encoded><![CDATA[<p>Scala lets you improve this a bit with a self type annotation. I don&#8217;t find the type parameter that ugly (after all, you need to get the actual type in there somehow), but the unchecked casts are nasty. Here&#8217;s the pattern in Scala:</p>
<p>class BaseBuilder[T<br />
    def add(): T = {<br />
        //...<br />
        this<br />
    }<br />
    def divide(): T = {<br />
        //...<br />
        this<br />
    }<br />
    def multiply(): T = {<br />
        //...<br />
        this<br />
    }<br />
}</p>
<p>class FooBuilder extends BaseBuilder[FooBuilder] {<br />
}</p>
<p>The &#8220;this: T =&gt;&#8221; in the first line is the key. It says to treat &#8220;this&#8221; as having a static type of T rather than BaseBuilder. If anybody tries to subclass BaseBuilder without satisfying that requirement, it&#8217;s a compile error. (You can get the same effect by just doing &#8220;class BaseBuilder[T] { this: T =&gt; &#8230; }&#8221;, which just leaves off the bounds on T, but the error messages are uglier.) Anyway it basically allows T to act just like your ME type, and the compiler enforces it.</p>
<p>Incidentally, I&#8217;d make a few other changes if I were doing it in Scala: I&#8217;d make BaseBuilder a trait and clean up the syntax of the methods a bit. But basically that&#8217;s the idea&#8230;</p>
]]></content:encoded>
	</item>
</channel>
</rss>

