I think this idea is a stairway to duck typing.<br>I exagerate, of course, but here is my point:<br><br>It shouldn't be difficult to make a class:<br><span style="font-family: courier new,monospace;">class HasName a where</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> name :: a -> String</span><br><br>The problem is when declaring Foo and Bar instances of HasName, since you have to copy code :<br><span style="font-family: courier new,monospace;">data Foo = Foo String</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">data Bar = Bar String</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">instance HasName Foo where</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> name (Foo n) = n</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">instance HasName Bar where</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> name (Bar n) = n</span><br><br>I'm sure one can automatize this using TemplateHaskell, but it is not really simple.<br><br>What I mean is that GHC should give a means to automatize this kind of situation, for instance:<br>
data Foo = Foo { name :: String }<br> (deriving HasName)<br><br>Or even:<br>data Foo = Foo { HasName.name }<br><br>Just an idea.<br><br><div class="gmail_quote">2010/11/10 Ozgur Akgun <span dir="ltr"><<a href="mailto:ozgurakgun@gmail.com">ozgurakgun@gmail.com</a>></span><br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div>I still don't know whether I like this idea or not, but here is the simplest definition I can think of about what it promises.</div>
<div><br></div>Using TDNR, it will be possible to write the following code:<div><br>
</div><div><div><font face="'courier new', monospace">data Foo = Foo { name :: String }</font></div><div><font face="'courier new', monospace">data Bar = Bar { name :: String }</font></div>
<div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace">getName :: Either Foo Bar -> String</font></div><div>
<font face="'courier new', monospace">getName (Left f) = name f</font></div><div><font face="'courier new', monospace">getName (Right b) = name b</font></div>
<div><br></div><div>However, currently you cannot: "Multiple declarations of 'name'"</div><div><br></div><div>There are basically two things you can do to solve this "problem".</div><div> - Use different names for your functions, a la "fooName, barName". This clutters up your code, and sometimes you may not have access to that part of the code.</div>
<div> - Define these 2 data types in different modules and import qualified, a la "Foo.name, Bar.name". One might still think this clutters up your code.</div><div><br></div><div>In any case, as a programmer you need to resolve which function to use depending on types while defining 'getName'. However compiler has enough information to automate this decision for you. This is not a way to do polymorphism, this is merely a way to allow programmers define more than one function with the same name, but different types.</div>
<div><br></div><div>This kinda sounds like what java people think polymorphism is :P</div><br>-- <br>Ozgur Akgun<br>
</div>
<br>_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
<br></blockquote></div><br>