<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.2.1" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: Duck Typing vs Readability</title>
	<link>http://www.alittlemadness.com/2006/06/21/duck-typing-vs-readability/</link>
	<description>A man needs a little madness, or else he never dares cut the rope and be free. -Nikos Kazantzakis</description>
	<pubDate>Wed, 07 Jan 2009 03:12:38 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.1</generator>

	<item>
		<title>By: a little madness &#187; The Key Thing In Python&#8217;s Favour</title>
		<link>http://www.alittlemadness.com/2006/06/21/duck-typing-vs-readability/#comment-38778</link>
		<author>a little madness &#187; The Key Thing In Python&#8217;s Favour</author>
		<pubDate>Tue, 04 Sep 2007 15:00:28 +0000</pubDate>
		<guid>http://www.alittlemadness.com/2006/06/21/duck-typing-vs-readability/#comment-38778</guid>
		<description>[...] languages favour writers over readers. I covered one example of this problem back in my post on Duck Typing vs Readability. This is why it is so important that a dynamic language is designed with readability in [...]</description>
		<content:encoded><![CDATA[<p>[&#8230;] languages favour writers over readers. I covered one example of this problem back in my post on Duck Typing vs Readability. This is why it is so important that a dynamic language is designed with readability in [&#8230;]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kareem</title>
		<link>http://www.alittlemadness.com/2006/06/21/duck-typing-vs-readability/#comment-1622</link>
		<author>Kareem</author>
		<pubDate>Thu, 07 Sep 2006 01:49:33 +0000</pubDate>
		<guid>http://www.alittlemadness.com/2006/06/21/duck-typing-vs-readability/#comment-1622</guid>
		<description>I have a wish for a sort of duck-typing as described by the OP. I want to be able to specify a "type-scope" for variables, including parameters. The scope is specified for a name and the types that name can be treated as.

Assuming types have methods (and maybe properties, events, etc.) the scope would say, "For this type, only allow that which is common to all the types specified, or if in a type-scope (recursive case), further restrict what can be done." In other words, take the intersection of methods (or whatever) for all the types specified, and that set is what is strongly-typed checked. Or in plainer terms, if it could work, let it work. (Casting can be done, and when that cast fails, throw a run-time exception.)

It would look something like this (I am using a C-like syntax):

a as String, List, b as float, int, Imaginary, Matrix
{
  a.Size; // Ok.
  a.Reverse(); // Ok
  a.ToUpper(); // Compiler error: List does not implement ToUpper.
  b.Absolute(); // Ok
  b.Invert(); // Compiler error: float, int, Imaginary do not implement ToUpper.
  a as String
  {
    a.ToUpper(); // Now this is OK as a is only a String
  }
  if a is String
  {
     a.ToUpper(); // This is OK to as a is only a String
  }
  Method2(a); // This will be ok if Method2 takes one paramter of type
              // String or List

In effect, a limited type-inference is being done. Not only is the type considered, but so is the method or property or whatever is being invoked on/with the object. One can imagine the declaration being a list of methods (including the types) that determine if a type will pass static-type checking.

I think I would prefer the idea of the OP, which is the same idea that I have wanted for a while, but this seems like a "more type-safe" compromise.

While I am at it, how about declarations like this: *.ToString(), *.DoesThis(), *.HasThis, etc. This would save having to implement interfaces for the sake of being able to do this.

As for my background in relation to dynamic typed languages:

I have used Python (after much C/C++) coding, and despite my fears about no static typing, I found I was able to code productivily, correctly and quickly while feeling that the language was not getting in my way nearly as much as C/C++ did.
 
I was suprised that my mind set seemed to change in that I quickly adpated to the lack of strong typing and became more careful somehow such that I did not have anywhere near the number type errors that I was predicting.
 
One rule that I have heard is "strong testing not strong typing", which I think in the end must be true, but I think strong typing still is helpful for its ability to help me to try to do what I want to do.
 
I am now using C# and really like that language. However, I miss multiple-inheritance and especially the dynamic typing. I don't miss as much things like changing the signature of a type. Oh, and indenting for blocks, I really liked that too.</description>
		<content:encoded><![CDATA[<p>I have a wish for a sort of duck-typing as described by the OP. I want to be able to specify a &#8220;type-scope&#8221; for variables, including parameters. The scope is specified for a name and the types that name can be treated as.</p>
<p>Assuming types have methods (and maybe properties, events, etc.) the scope would say, &#8220;For this type, only allow that which is common to all the types specified, or if in a type-scope (recursive case), further restrict what can be done.&#8221; In other words, take the intersection of methods (or whatever) for all the types specified, and that set is what is strongly-typed checked. Or in plainer terms, if it could work, let it work. (Casting can be done, and when that cast fails, throw a run-time exception.)</p>
<p>It would look something like this (I am using a C-like syntax):</p>
<p>a as String, List, b as float, int, Imaginary, Matrix<br />
{<br />
  a.Size; // Ok.<br />
  a.Reverse(); // Ok<br />
  a.ToUpper(); // Compiler error: List does not implement ToUpper.<br />
  b.Absolute(); // Ok<br />
  b.Invert(); // Compiler error: float, int, Imaginary do not implement ToUpper.<br />
  a as String<br />
  {<br />
    a.ToUpper(); // Now this is OK as a is only a String<br />
  }<br />
  if a is String<br />
  {<br />
     a.ToUpper(); // This is OK to as a is only a String<br />
  }<br />
  Method2(a); // This will be ok if Method2 takes one paramter of type<br />
              // String or List</p>
<p>In effect, a limited type-inference is being done. Not only is the type considered, but so is the method or property or whatever is being invoked on/with the object. One can imagine the declaration being a list of methods (including the types) that determine if a type will pass static-type checking.</p>
<p>I think I would prefer the idea of the OP, which is the same idea that I have wanted for a while, but this seems like a &#8220;more type-safe&#8221; compromise.</p>
<p>While I am at it, how about declarations like this: *.ToString(), *.DoesThis(), *.HasThis, etc. This would save having to implement interfaces for the sake of being able to do this.</p>
<p>As for my background in relation to dynamic typed languages:</p>
<p>I have used Python (after much C/C++) coding, and despite my fears about no static typing, I found I was able to code productivily, correctly and quickly while feeling that the language was not getting in my way nearly as much as C/C++ did.</p>
<p>I was suprised that my mind set seemed to change in that I quickly adpated to the lack of strong typing and became more careful somehow such that I did not have anywhere near the number type errors that I was predicting.</p>
<p>One rule that I have heard is &#8220;strong testing not strong typing&#8221;, which I think in the end must be true, but I think strong typing still is helpful for its ability to help me to try to do what I want to do.</p>
<p>I am now using C# and really like that language. However, I miss multiple-inheritance and especially the dynamic typing. I don&#8217;t miss as much things like changing the signature of a type. Oh, and indenting for blocks, I really liked that too.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: verisimilidude</title>
		<link>http://www.alittlemadness.com/2006/06/21/duck-typing-vs-readability/#comment-307</link>
		<author>verisimilidude</author>
		<pubDate>Wed, 05 Jul 2006 18:25:08 +0000</pubDate>
		<guid>http://www.alittlemadness.com/2006/06/21/duck-typing-vs-readability/#comment-307</guid>
		<description>A couple of observations:

I once worked on a Smalltalk GUI project where I was responsible for some C++ work that implemented a capatible interface for things (network and DBMS) that needed to be called by smalltalk but for which the C-callable libraries needed a nicer wrapper.  In three months the (experienced) smalltalk developers delivered code for a user application that I would have estimated would take 9 months to develop in C++ or Java.  I then switched to testing.  After 6 months we were still finding bugs, most relating to the extensive use of duck typing that the developers had engaged in.

While C++ templates might seem the answer the current state of the art leaves much to be desired.  When they work they work well.  It is in development when you unwittingly pass in a class and get an error message 100 lines long.  Someday it will be possible for the compiler to tell you "Template Foo called from Template Bar at line 999 of the Fraz routine requires that class Baz implement a function iterator()".  Figuring this out from the error messages C++ compilers today give is almost impossible.  At least Python gives you a stack crawl that gives you the information at a glance even if is already runtime.</description>
		<content:encoded><![CDATA[<p>A couple of observations:</p>
<p>I once worked on a Smalltalk GUI project where I was responsible for some C++ work that implemented a capatible interface for things (network and DBMS) that needed to be called by smalltalk but for which the C-callable libraries needed a nicer wrapper.  In three months the (experienced) smalltalk developers delivered code for a user application that I would have estimated would take 9 months to develop in C++ or Java.  I then switched to testing.  After 6 months we were still finding bugs, most relating to the extensive use of duck typing that the developers had engaged in.</p>
<p>While C++ templates might seem the answer the current state of the art leaves much to be desired.  When they work they work well.  It is in development when you unwittingly pass in a class and get an error message 100 lines long.  Someday it will be possible for the compiler to tell you &#8220;Template Foo called from Template Bar at line 999 of the Fraz routine requires that class Baz implement a function iterator()&#8221;.  Figuring this out from the error messages C++ compilers today give is almost impossible.  At least Python gives you a stack crawl that gives you the information at a glance even if is already runtime.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Simon Clarkstone</title>
		<link>http://www.alittlemadness.com/2006/06/21/duck-typing-vs-readability/#comment-206</link>
		<author>Simon Clarkstone</author>
		<pubDate>Fri, 23 Jun 2006 08:13:04 +0000</pubDate>
		<guid>http://www.alittlemadness.com/2006/06/21/duck-typing-vs-readability/#comment-206</guid>
		<description>I am suprised those who have mentioned Haskell have not mentioned typeclasses.  In Haskell, variables have types.  Those types may belond to one or more typeclasses.  A typeclass is like a Java interface, but you can require more than one of them for a type, they can have default method implementations, and most importantly you can add them to existing types without having to touch the original type definition.  This means that you can have a typeclass that is local to a module without bothering other modules.  Some of the typeclasses from the standard library have language support, by being derivable by the compiler or having special syntax.

Built-in classes include:

Eq -- provides == and /=
Ord -- requires Eq, and also has comparison operators
Enum -- for enumerable types
Num -- requires Ord and Enum, provides the most fundamental numeric support
Show -- has the show method which is very like Java's toString()
Read -- the inverse of Show: has a read method to read the type from a string.  Note how dispatch to the correct implementation of read is based solely on the required *return* type, not the data in the string -- something you cannot do in Java or Python
Monad -- difficult to explain, it's related to the ability to hide repetative plumbing code, and allows use of the "do"-notation for syntactic sugar

See the Haskell report for more on this.</description>
		<content:encoded><![CDATA[<p>I am suprised those who have mentioned Haskell have not mentioned typeclasses.  In Haskell, variables have types.  Those types may belond to one or more typeclasses.  A typeclass is like a Java interface, but you can require more than one of them for a type, they can have default method implementations, and most importantly you can add them to existing types without having to touch the original type definition.  This means that you can have a typeclass that is local to a module without bothering other modules.  Some of the typeclasses from the standard library have language support, by being derivable by the compiler or having special syntax.</p>
<p>Built-in classes include:</p>
<p>Eq &#8212; provides == and /=<br />
Ord &#8212; requires Eq, and also has comparison operators<br />
Enum &#8212; for enumerable types<br />
Num &#8212; requires Ord and Enum, provides the most fundamental numeric support<br />
Show &#8212; has the show method which is very like Java&#8217;s toString()<br />
Read &#8212; the inverse of Show: has a read method to read the type from a string.  Note how dispatch to the correct implementation of read is based solely on the required *return* type, not the data in the string &#8212; something you cannot do in Java or Python<br />
Monad &#8212; difficult to explain, it&#8217;s related to the ability to hide repetative plumbing code, and allows use of the &#8220;do&#8221;-notation for syntactic sugar</p>
<p>See the Haskell report for more on this.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Angry Programmer</title>
		<link>http://www.alittlemadness.com/2006/06/21/duck-typing-vs-readability/#comment-202</link>
		<author>Angry Programmer</author>
		<pubDate>Thu, 22 Jun 2006 10:06:58 +0000</pubDate>
		<guid>http://www.alittlemadness.com/2006/06/21/duck-typing-vs-readability/#comment-202</guid>
		<description>Duck typing is dangerous? What the hell is that supposed to mean? Duck typing is not for hardware driver implementations, yes, but do you know how they always say one shouldn't run with scissors?</description>
		<content:encoded><![CDATA[<p>Duck typing is dangerous? What the hell is that supposed to mean? Duck typing is not for hardware driver implementations, yes, but do you know how they always say one shouldn&#8217;t run with scissors?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: yozzeff</title>
		<link>http://www.alittlemadness.com/2006/06/21/duck-typing-vs-readability/#comment-194</link>
		<author>yozzeff</author>
		<pubDate>Wed, 21 Jun 2006 16:05:40 +0000</pubDate>
		<guid>http://www.alittlemadness.com/2006/06/21/duck-typing-vs-readability/#comment-194</guid>
		<description>actually i agree with most of what you say BUT there is some essential 
piece of information missing: WHEN?
when should i type or when should i not type. see my blog here;
http://yozzeff.blogspot.com/2006/03/static-typing-vs-dynamic-typing.html</description>
		<content:encoded><![CDATA[<p>actually i agree with most of what you say BUT there is some essential<br />
piece of information missing: WHEN?<br />
when should i type or when should i not type. see my blog here;<br />
<a href="http://yozzeff.blogspot.com/2006/03/static-typing-vs-dynamic-typing.html" rel="nofollow">http://yozzeff.blogspot.com/2006/03/static-typing-vs-dynamic-typing.html</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: The Angry Programmer &#187; More stuff to read</title>
		<link>http://www.alittlemadness.com/2006/06/21/duck-typing-vs-readability/#comment-193</link>
		<author>The Angry Programmer &#187; More stuff to read</author>
		<pubDate>Wed, 21 Jun 2006 15:43:04 +0000</pubDate>
		<guid>http://www.alittlemadness.com/2006/06/21/duck-typing-vs-readability/#comment-193</guid>
		<description>[...] http://www.alittlemadness.com/?p=28 [...]</description>
		<content:encoded><![CDATA[<p>[&#8230;] <a href="http://www.alittlemadness.com/?p=28" rel="nofollow">http://www.alittlemadness.com/?p=28</a> [&#8230;]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ivan</title>
		<link>http://www.alittlemadness.com/2006/06/21/duck-typing-vs-readability/#comment-191</link>
		<author>Ivan</author>
		<pubDate>Wed, 21 Jun 2006 13:55:38 +0000</pubDate>
		<guid>http://www.alittlemadness.com/2006/06/21/duck-typing-vs-readability/#comment-191</guid>
		<description>btw, nice antlr articles :)</description>
		<content:encoded><![CDATA[<p>btw, nice antlr articles <img src='http://www.alittlemadness.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ivan</title>
		<link>http://www.alittlemadness.com/2006/06/21/duck-typing-vs-readability/#comment-190</link>
		<author>Ivan</author>
		<pubDate>Wed, 21 Jun 2006 13:54:12 +0000</pubDate>
		<guid>http://www.alittlemadness.com/2006/06/21/duck-typing-vs-readability/#comment-190</guid>
		<description>Duck typing is flat out dangerous, and I think you can leave it at that.  There is a benefit in terms of not dealing with "hassling" conversons, but I find that is less of an issue these days when LOC required to code is no longer directly a measure of potential productivity.  

In my experience is definitely more fun to type, but not worth the extra effort in terms of code maint. as the project gets better.  As a lazy programmer, IDE's should be doing my language testing and correction.  I should be doing only the logical errors.

In practice, I find ironically that static typing allows me to think at a higher level and stay there.  I know I'm not the only programmer who feels that way.</description>
		<content:encoded><![CDATA[<p>Duck typing is flat out dangerous, and I think you can leave it at that.  There is a benefit in terms of not dealing with &#8220;hassling&#8221; conversons, but I find that is less of an issue these days when LOC required to code is no longer directly a measure of potential productivity.  </p>
<p>In my experience is definitely more fun to type, but not worth the extra effort in terms of code maint. as the project gets better.  As a lazy programmer, IDE&#8217;s should be doing my language testing and correction.  I should be doing only the logical errors.</p>
<p>In practice, I find ironically that static typing allows me to think at a higher level and stay there.  I know I&#8217;m not the only programmer who feels that way.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: x</title>
		<link>http://www.alittlemadness.com/2006/06/21/duck-typing-vs-readability/#comment-188</link>
		<author>x</author>
		<pubDate>Wed, 21 Jun 2006 12:47:46 +0000</pubDate>
		<guid>http://www.alittlemadness.com/2006/06/21/duck-typing-vs-readability/#comment-188</guid>
		<description>The Perils of Duck Typing:

http://www.beust.com/weblog/archives/000269.html</description>
		<content:encoded><![CDATA[<p>The Perils of Duck Typing:</p>
<p><a href="http://www.beust.com/weblog/archives/000269.html" rel="nofollow">http://www.beust.com/weblog/archives/000269.html</a></p>
]]></content:encoded>
	</item>
</channel>
</rss>
