<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On 2008 May 18, at 9:59, Kaveh Shahbazian wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">For something like: type Thing a b = ThisWay a | ThatWay b | NoWay actually there is no equivalents for data constructor </blockquote><div><br></div><div>I presume you mean "data" instead of "type". Not that I can address your question directly, as I don't know C#. In C it's a union; in C++ you would export constructors ThisWay(), ThatWay(), NoWay() from class Thing. I presume C# is similar. Haskell syntax is decidedly more compact than any of them.</div><br><blockquote type="cite">in C# 3 (I think). I asked it if there are other ideas about this: Controlling the execution path by deciding based of structure of data with a trick other than reflecting!</blockquote><br></div><div>in C-like languages, the class includes a structure tag which is set by the constructor. Guess what? That's how Haskell does it, just implicitly (hence, again, nicely compact syntax). (You can actually experiment with this in GHC; using internal stuff like UnsafeCoerce# you can coerce one datum to another if they have the same (0-based, assigned in order of definition) constructor tag and the same representation for the data value. IIRC in GHC the internal constructor tag is an 8-bit unsigned value.)</div><div><br></div><div>To make the above a bit clearer (I hope), here's a rough C approximation of a simple Haskell type:</div><div><br></div><div> /* data Foo = FooInt Int | FooDouble Double */</div><div><br></div><div> struct Foo {</div><div> unsigned char _FooTag;</div><div> union {</div><div> #define _FooTag_FooInt 0</div><div> int _FooInt;</div><div> #define _FooTag_FooDouble 1</div><div> double _FooDouble;</div><div> } _Foo_u;</div><div> };</div><div><br></div><div> /* here I assume unboxed basic types for simplicity */</div><div> struct Foo *FooInt(int param) {</div><div> Foo *foo = malloc(sizeof *foo); /* assume sane error checking here */</div><div> foo->_FooTag = _FooTag_FooInt;</div><div> foo->_Foo_u._FooInt = param;</div><div> }</div><div><font class="Apple-style-span" face="Monaco" size="3"><span class="Apple-style-span" style="font-size: 11px;"><br></span></font></div><div><font class="Apple-style-span" face="Monaco" size="3"><span class="Apple-style-span" style="font-size: 11px;"><span class="Apple-style-span" style="font-family: Arial; font-size: 12px; "><div> struct Foo *FooDouble(int param) {</div><div> Foo *foo = malloc(sizeof *foo); /* assume sane error checking here */</div><div> foo->_FooTag = _FooTag_FooDouble;</div><div> foo->_Foo_u._FooDouble = param;</div><div> }</div><div><br></div><div> /*</div><div> * bar (FooInt i) = ...</div><div> * desugars to</div><div> * bar x = case x of { FooInt i -> ... }</div><div> * <span class="Apple-tab-span" style="white-space:pre">        </span>which is very roughly (pretend I catch x == 0 and invoke error("Undefined"))</div><div> * bar(Foo x) { switch (x->_FooTag) { case _FooTag_FooInt: i = x->_Foo_u._FooInt; ... } }</div><div> */</div><div><br></div></span></span></font></div><div><span class="Apple-style-span" style="font-family: Monaco; font-size: 11px; ">-- </span></div><div apple-content-edited="true"><span class="Apple-style-span" style="border-collapse: separate; border-spacing: 0px 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 11px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-align: auto; -khtml-text-decorations-in-effect: none; text-indent: 0px; -apple-text-size-adjust: auto; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; "><div style="word-wrap: break-word; -khtml-nbsp-mode: space; -khtml-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; border-spacing: 0px 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 11px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-align: auto; -khtml-text-decorations-in-effect: none; text-indent: 0px; -apple-text-size-adjust: auto; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; "><span class="Apple-style-span" style="border-collapse: separate; border-spacing: 0px 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 11px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-align: auto; -khtml-text-decorations-in-effect: none; text-indent: 0px; -apple-text-size-adjust: auto; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; "><div><font class="Apple-style-span" face="Monaco"><span class="Apple-style-span" style="font-family: Monaco; "><span class="Apple-style-span" style="font-family: Monaco; ">brandon s. allbery [solaris,freebsd,perl,pugs,haskell] <a href="mailto:allbery@kf8nh.com">allbery@kf8nh.com</a></span></span></font></div><div><font class="Apple-style-span" face="Monaco"><span class="Apple-style-span" style="font-family: Monaco; "><span class="Apple-style-span" style="font-family: Monaco; ">system administrator [openafs,heimdal,too many hats] <a href="mailto:allbery@ece.cmu.edu">allbery@ece.cmu.edu</a></span></span></font></div><div><font class="Apple-style-span" face="Monaco"><span class="Apple-style-span" style="font-family: Monaco; "><span class="Apple-style-span" style="font-family: Monaco; ">electrical and computer engineering, carnegie mellon university KF8NH</span></span></font></div><span class="Apple-style-span" style="border-collapse: separate; border-spacing: 0px 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 11px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-align: auto; -khtml-text-decorations-in-effect: none; text-indent: 0px; -apple-text-size-adjust: auto; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; "><br class="Apple-interchange-newline"></span></span></span></div></span> </div><br></body></html>