<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.0.5" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: Algebraic Data Types again</title>
	<link>http://blog.tmorris.net/algebraic-data-types-again/</link>
	<description>The weblog of Tony Morris</description>
	<pubDate>Sat, 19 Jul 2008 23:34:03 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.5</generator>

	<item>
		<title>by: Niklas Matthies</title>
		<link>http://blog.tmorris.net/algebraic-data-types-again/#comment-9495</link>
		<pubDate>Fri, 17 Aug 2007 23:05:24 +0000</pubDate>
		<guid>http://blog.tmorris.net/algebraic-data-types-again/#comment-9495</guid>
					<description>One way to model these in Java (and similar languages) is the Visitor pattern. For example:
    interface Either&#60;A, B&#62; {
        void accept(Visitor&#60;A, B&#62; visitor);
        interface Visitor&#60;A, B&#62; {
            void visit(A a);
            void visit(B b);
        }
    }

This effectively forces instances of Either to either hold an A or a B (unless they consciously break the contract associated with the accept() method), and client code gets pattern matching "for free" by implementing suitable Visitors. Of course it's much more verbose than in Haskell and ML, but nevertheless quite useful. No need to trouble oneself with instanceof, casts and non-exhaustive matches.

As for Maybe, I have this "library solution" in Java:
    interface Optional&#60;T&#62; {
        T orNull();
        T orElse(T defaultValue);
        T orThrow() throws RuntimeException;
        boolean isSome();
        boolean isNone();
    }
Usage:
    class Test {
        static Optional&#60;Number&#62; getNumber() { ... }

        public static void main(String[] args) {
            Number a = getNumber().orNull();
            Number b = getNumber().orElse(Double.NaN);
            Number c = getNumber().orThrow();
            Optional&#60;Number&#62; d = getNumber();
            if (d.isSome()) System.out.println(d.orThrow());
        }
    }
</description>
		<content:encoded><![CDATA[<p>One way to model these in Java (and similar languages) is the Visitor pattern. For example:<br />
    interface Either&lt;A, B&gt; {<br />
        void accept(Visitor&lt;A, B&gt; visitor);<br />
        interface Visitor&lt;A, B&gt; {<br />
            void visit(A a);<br />
            void visit(B b);<br />
        }<br />
    }</p>
<p>This effectively forces instances of Either to either hold an A or a B (unless they consciously break the contract associated with the accept() method), and client code gets pattern matching &#8220;for free&#8221; by implementing suitable Visitors. Of course it&#8217;s much more verbose than in Haskell and ML, but nevertheless quite useful. No need to trouble oneself with instanceof, casts and non-exhaustive matches.</p>
<p>As for Maybe, I have this &#8220;library solution&#8221; in Java:<br />
    interface Optional&lt;T&gt; {<br />
        T orNull();<br />
        T orElse(T defaultValue);<br />
        T orThrow() throws RuntimeException;<br />
        boolean isSome();<br />
        boolean isNone();<br />
    }<br />
Usage:<br />
    class Test {<br />
        static Optional&lt;Number&gt; getNumber() { &#8230; }</p>
<p>        public static void main(String[] args) {<br />
            Number a = getNumber().orNull();<br />
            Number b = getNumber().orElse(Double.NaN);<br />
            Number c = getNumber().orThrow();<br />
            Optional&lt;Number&gt; d = getNumber();<br />
            if (d.isSome()) System.out.println(d.orThrow());<br />
        }<br />
    }
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: jhorman.org - links for 2007-08-06</title>
		<link>http://blog.tmorris.net/algebraic-data-types-again/#comment-9053</link>
		<pubDate>Mon, 06 Aug 2007 23:27:48 +0000</pubDate>
		<guid>http://blog.tmorris.net/algebraic-data-types-again/#comment-9053</guid>
					<description>[...] Algebraic Data Types again (tags: haskell) [...]</description>
		<content:encoded><![CDATA[<p>[&#8230;] Algebraic Data Types again (tags: haskell) [&#8230;]
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Thomas</title>
		<link>http://blog.tmorris.net/algebraic-data-types-again/#comment-8973</link>
		<pubDate>Sun, 05 Aug 2007 16:52:20 +0000</pubDate>
		<guid>http://blog.tmorris.net/algebraic-data-types-again/#comment-8973</guid>
					<description>Note that the GHC Haskell Compiler can also warn you about overlapping or missing pattern matches or patterns that cannot be matched, because some pattern before that would match all those patterns already.  The compiler switch to enable this is naturally called -Wall and is considered good practice to be used (of course).</description>
		<content:encoded><![CDATA[<p>Note that the GHC Haskell Compiler can also warn you about overlapping or missing pattern matches or patterns that cannot be matched, because some pattern before that would match all those patterns already.  The compiler switch to enable this is naturally called -Wall and is considered good practice to be used (of course).
</p>
]]></content:encoded>
				</item>
</channel>
</rss>
