<?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: Almost Useful: Java Type Intersection.</title>
	<atom:link href="http://virtualinfinity.net/wordpress/java/esoteric-java-features/2007/11/23/type-intersection/feed/" rel="self" type="application/rss+xml" />
	<link>http://virtualinfinity.net/wordpress/java/esoteric-java-features/2007/11/23/type-intersection/</link>
	<description>Daniel Pitts' Tech Blog</description>
	<pubDate>Sun, 07 Sep 2008 15:33:52 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5</generator>
		<item>
		<title>By: Piotr Kobzda</title>
		<link>http://virtualinfinity.net/wordpress/java/esoteric-java-features/2007/11/23/type-intersection/#comment-83</link>
		<dc:creator>Piotr Kobzda</dc:creator>
		<pubDate>Wed, 12 Dec 2007 19:10:49 +0000</pubDate>
		<guid isPermaLink="false">http://virtualinfinity.net/wordpress/java/esoteric-java-features/2007/11/23/type-intersection/#comment-83</guid>
		<description>Yes, we shouldn't have to do that.  However, we can!

The reason intersection types are not implemented for direct use is in the way the JVM works, and Java 5 is chosen to be implemented.  Currently, there is no special runtime type to represent intersection type, it's always compiled as the erasure of its first bound.

Implementing it in a different way most probably requires some changes in the JVM and/or generation of synthetic classes.  From a number of reasons it was a change the Java architects wanted to avoid.  But, who knows?  Maybe future versions will support that?...


P.S.  Thank you for formatting my previous comment, but there are still some incompleteness in my code caused by a replay form.

In my original example a reference holder was declared as:
&lt;pre class="code"&gt;
public class ListRef&#60;T extends List&#60;String&#62; &#38; RandomAccess&#62; {
    public final T value;

    public ListRef(T value) {
        this.value = value;
    }
}
&lt;/pre&gt;
Example method implementation as:
&lt;pre class="code"&gt;
public ListRef&#60;?&#62; foo() {
    return new ListRef&#60;ArrayList&#60;String&#62;&#62;(new ArrayList&#60;String&#62;());
}
&lt;/pre&gt;
And sample use snippet as:
&lt;pre class="code"&gt;
ListRef&#60;?&#62; listRef = foo();
List&#60;String&#62; list = listRef.value;
RandomAccess ra = listRef.value;
...
&lt;/pre&gt;
</description>
		<content:encoded><![CDATA[<p>Yes, we shouldn&#8217;t have to do that.  However, we can!</p>
<p>The reason intersection types are not implemented for direct use is in the way the JVM works, and Java 5 is chosen to be implemented.  Currently, there is no special runtime type to represent intersection type, it&#8217;s always compiled as the erasure of its first bound.</p>
<p>Implementing it in a different way most probably requires some changes in the JVM and/or generation of synthetic classes.  From a number of reasons it was a change the Java architects wanted to avoid.  But, who knows?  Maybe future versions will support that?&#8230;</p>
<p>P.S.  Thank you for formatting my previous comment, but there are still some incompleteness in my code caused by a replay form.</p>
<p>In my original example a reference holder was declared as:</p>
<pre class="code">
public class ListRef&lt;T extends List&lt;String&gt; &amp; RandomAccess&gt; {
    public final T value;

    public ListRef(T value) {
        this.value = value;
    }
}
</pre>
<p>Example method implementation as:</p>
<pre class="code">
public ListRef&lt;?&gt; foo() {
    return new ListRef&lt;ArrayList&lt;String&gt;&gt;(new ArrayList&lt;String&gt;());
}
</pre>
<p>And sample use snippet as:</p>
<pre class="code">
ListRef&lt;?&gt; listRef = foo();
List&lt;String&gt; list = listRef.value;
RandomAccess ra = listRef.value;
...
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel</title>
		<link>http://virtualinfinity.net/wordpress/java/esoteric-java-features/2007/11/23/type-intersection/#comment-82</link>
		<dc:creator>Daniel</dc:creator>
		<pubDate>Wed, 12 Dec 2007 18:09:58 +0000</pubDate>
		<guid isPermaLink="false">http://virtualinfinity.net/wordpress/java/esoteric-java-features/2007/11/23/type-intersection/#comment-82</guid>
		<description>Hmm, thats interesting, but it is still a hack that shouldn't have to be. Anyway, I think I would have this:
&lt;pre class="code"&gt;
public class RandomAccessListDelegate&#60;E, T extends &#60;List&#60;E&gt; &#038; RandomAccess&gt; implements RandomAccess, List&#60;E&gt; {
  private final T delegate;
  private RandomAccessListDelegate(T delegate) {
    this.delegate = delegate;
  }
  public static &#60;E, T extends &#60;List&#60;E&gt; &#038; RandomAccess&gt; RandomAccessListDelegate&#60;E, T&gt; create(T delegate) {
    return new RandomAccessListDelegate&lt;E, T&gt;(delegate);
  }
  public T getDelegate() { return delegate; }
  // Delegate all methods to delegate
  // [code snipped]
}
&lt;/pre&gt;
Still, I shouldn't have to jump this hoop.</description>
		<content:encoded><![CDATA[<p>Hmm, thats interesting, but it is still a hack that shouldn&#8217;t have to be. Anyway, I think I would have this:</p>
<pre class="code">
public class RandomAccessListDelegate&lt;E, T extends &lt;List&lt;E> &#038; RandomAccess> implements RandomAccess, List&lt;E> {
  private final T delegate;
  private RandomAccessListDelegate(T delegate) {
    this.delegate = delegate;
  }
  public static &lt;E, T extends &lt;List&lt;E> &#038; RandomAccess> RandomAccessListDelegate&lt;E, T> create(T delegate) {
    return new RandomAccessListDelegate<e , T>(delegate);
  }
  public T getDelegate() { return delegate; }
  // Delegate all methods to delegate
  // [code snipped]
}
</e></pre>
<p>Still, I shouldn&#8217;t have to jump this hoop.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Piotr Kobzda</title>
		<link>http://virtualinfinity.net/wordpress/java/esoteric-java-features/2007/11/23/type-intersection/#comment-80</link>
		<dc:creator>Piotr Kobzda</dc:creator>
		<pubDate>Wed, 12 Dec 2007 10:12:45 +0000</pubDate>
		<guid isPermaLink="false">http://virtualinfinity.net/wordpress/java/esoteric-java-features/2007/11/23/type-intersection/#comment-80</guid>
		<description>Daniel, note that current Java do not allows for direct use of intersection types as part of the program.  However, you can always use a wildcard type as a reference holder of any intersection type instance you wish.

For example, having a reference holder declared as:
&lt;pre class="code"&gt;
public class ListRef&#60;T extends List &#38; RandomAccess&#62; {
    public final T value;

    public ListRef(T value) {
        this.value = value;
    }
}
&lt;/pre&gt;
you can return any implementation matching declared intersection type this way:
&lt;pre class="code"&gt;
public ListRef foo() {
   return new ListRef&#60;ArrayList&#62;(new ArrayList());
}
&lt;/pre&gt;
and use it as follows:
&lt;pre class="code"&gt;
ListRef listRef = foo();
List list = listRef.value;
RandomAccess ra = listRef.value;
...
&lt;/pre&gt;

I think, it is a bit more than just "Almost Useful" Java feature, isn't it? :-)</description>
		<content:encoded><![CDATA[<p>Daniel, note that current Java do not allows for direct use of intersection types as part of the program.  However, you can always use a wildcard type as a reference holder of any intersection type instance you wish.</p>
<p>For example, having a reference holder declared as:</p>
<pre class="code">
public class ListRef&lt;T extends List &amp; RandomAccess&gt; {
    public final T value;

    public ListRef(T value) {
        this.value = value;
    }
}
</pre>
<p>you can return any implementation matching declared intersection type this way:</p>
<pre class="code">
public ListRef foo() {
   return new ListRef&lt;ArrayList&gt;(new ArrayList());
}
</pre>
<p>and use it as follows:</p>
<pre class="code">
ListRef listRef = foo();
List list = listRef.value;
RandomAccess ra = listRef.value;
...
</pre>
<p>I think, it is a bit more than just &#8220;Almost Useful&#8221; Java feature, isn&#8217;t it? <img src='http://virtualinfinity.net/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel</title>
		<link>http://virtualinfinity.net/wordpress/java/esoteric-java-features/2007/11/23/type-intersection/#comment-76</link>
		<dc:creator>Daniel</dc:creator>
		<pubDate>Tue, 11 Dec 2007 16:54:08 +0000</pubDate>
		<guid isPermaLink="false">http://virtualinfinity.net/wordpress/java/esoteric-java-features/2007/11/23/type-intersection/#comment-76</guid>
		<description>You cant have a ? return type, can you?</description>
		<content:encoded><![CDATA[<p>You cant have a ? return type, can you?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aviad Ben Dov</title>
		<link>http://virtualinfinity.net/wordpress/java/esoteric-java-features/2007/11/23/type-intersection/#comment-74</link>
		<dc:creator>Aviad Ben Dov</dc:creator>
		<pubDate>Tue, 11 Dec 2007 12:06:09 +0000</pubDate>
		<guid isPermaLink="false">http://virtualinfinity.net/wordpress/java/esoteric-java-features/2007/11/23/type-intersection/#comment-74</guid>
		<description>Ah, I see now. I suppose I got confused as usually my return types are ? extends Something, which would allow this.</description>
		<content:encoded><![CDATA[<p>Ah, I see now. I suppose I got confused as usually my return types are ? extends Something, which would allow this.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel</title>
		<link>http://virtualinfinity.net/wordpress/java/esoteric-java-features/2007/11/23/type-intersection/#comment-72</link>
		<dc:creator>Daniel</dc:creator>
		<pubDate>Mon, 10 Dec 2007 16:24:42 +0000</pubDate>
		<guid isPermaLink="false">http://virtualinfinity.net/wordpress/java/esoteric-java-features/2007/11/23/type-intersection/#comment-72</guid>
		<description>&lt;code&gt;&#60;T extends List&#60;?&gt; &#038; RandomAccess&gt; T foo()&lt;/code&gt; has a return of type T, not ArrayList.  A caller is able to specify the type of T, which might not be ArrayList
&lt;pre class="code"&gt;
SomeRandomAccessListType list = myObject.&#60;SomeRandomAccessListType&gt;foo();
&lt;/pre&gt;
ArrayList is not a subclass of SomeRandomAccessListType, so you would get a cast error.</description>
		<content:encoded><![CDATA[<p><code>&lt;T extends List&lt;?> &#038; RandomAccess> T foo()</code> has a return of type T, not ArrayList.  A caller is able to specify the type of T, which might not be ArrayList</p>
<pre class="code">
SomeRandomAccessListType list = myObject.&lt;SomeRandomAccessListType>foo();
</pre>
<p>ArrayList is not a subclass of SomeRandomAccessListType, so you would get a cast error.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aviad Ben Dov</title>
		<link>http://virtualinfinity.net/wordpress/java/esoteric-java-features/2007/11/23/type-intersection/#comment-69</link>
		<dc:creator>Aviad Ben Dov</dc:creator>
		<pubDate>Mon, 10 Dec 2007 12:36:05 +0000</pubDate>
		<guid isPermaLink="false">http://virtualinfinity.net/wordpress/java/esoteric-java-features/2007/11/23/type-intersection/#comment-69</guid>
		<description>I'm sorry, you'll have to explain it again. Why can't you return "new ArrayList&#60;String&#62;()"? I mean, I tried it myself and saw it doesn't work, but I still don't get it. ArrayList DOES apply to the group of types that are both List AND RandomAccess... So why?</description>
		<content:encoded><![CDATA[<p>I&#8217;m sorry, you&#8217;ll have to explain it again. Why can&#8217;t you return &#8220;new ArrayList&lt;String&gt;()&#8221;? I mean, I tried it myself and saw it doesn&#8217;t work, but I still don&#8217;t get it. ArrayList DOES apply to the group of types that are both List AND RandomAccess&#8230; So why?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Pitts&#8217; Tech Blog &#187; Blog Archive &#187; Almost Useful: if instanceof</title>
		<link>http://virtualinfinity.net/wordpress/java/esoteric-java-features/2007/11/23/type-intersection/#comment-35</link>
		<dc:creator>Daniel Pitts&#8217; Tech Blog &#187; Blog Archive &#187; Almost Useful: if instanceof</dc:creator>
		<pubDate>Fri, 23 Nov 2007 22:33:13 +0000</pubDate>
		<guid isPermaLink="false">http://virtualinfinity.net/wordpress/java/esoteric-java-features/2007/11/23/type-intersection/#comment-35</guid>
		<description>[...] becomes even more useful if we combine the concept with the fully useful Type Intersection (Not yet implemented). If both of these features were implemented, then you could write something [...]</description>
		<content:encoded><![CDATA[<p>[&#8230;] becomes even more useful if we combine the concept with the fully useful Type Intersection (Not yet implemented). If both of these features were implemented, then you could write something [&#8230;]</p>
]]></content:encoded>
	</item>
</channel>
</rss>
