<?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"
	>
<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>
	<pubDate>Sat, 31 Jul 2010 06:51:56 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</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 'this' keyword is a compiler indicator to use the type of the variable. If b is a BaseBuilder, the method'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 'this') 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's tempting to introduce a special keyword like "ME" or "this" to mean something like "the concrete type of the eventual subclass." But I think you'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 "b.add().foo()" 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 "this" return type is that you really need to know, for example, that it's a FooBuilder: knowing that it's BaseBuilder isn't enough if foo() is only defined on FooBuilder.

This is the problem that the "extra" generic solves. If instead of BaseBuilder, you know that b is a BaseBuilder[FooBuilder], and instead of "ME" or "this" add returns a T (== FooBuilder, in this case), then the typechecker has the information that it needs to typecheck a call like "b.add().foo()". So in some sense the generic isn'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't care that the generic variable isn't filled out in the definition. At least Java doesn't care since it erases everything anyways and allows raw instances for generically typed classes.

The idea isn't that it can'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'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 &#60;: BaseBuilder[T]] { this: T =&#62; 

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'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'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 "this: T =&#62;" in the first line is the key. It says to treat "this" as having a static type of T rather than BaseBuilder. If anybody tries to subclass BaseBuilder without satisfying that requirement, it's a compile error. (You can get the same effect by just doing "class BaseBuilder[T] { this: T =&#62; ... }", 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'd make a few other changes if I were doing it in Scala: I'd make BaseBuilder a trait and clean up the syntax of the methods a bit. But basically that'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>
