<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Talk Like A Duck: Tag types</title>
    <link>http://talklikeaduck.denhaven2.com/articles/tag/types</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>In Ruby, it's not the dog, it's the tricks!</description>
    <item>
      <title>Yegge's At It Again!</title>
      <description>Another &lt;a href="http://steve-yegge.blogspot.com/2008/02/portrait-of-n00b.html"&gt;thought provoking piece from Steve Yegge.&lt;/a&gt;
&lt;blockquote&gt;Well, we also know that static types are just metadata. They&amp;#8217;re a specialized kind of comment targeted at two kinds of readers: programmers and compilers. Static types tell a story about the computation, presumably to help both reader groups understand the intent of the program. But the static types can be thrown away at runtime, because in the end they&amp;#8217;re just stylized comments. They&amp;#8217;re like pedigree paperwork: it might make a certain insecure personality type happier about their dog, but the dog certainly doesn&amp;#8217;t care. &amp;#8211; Steve Yegge&lt;/blockquote&gt;
&lt;p&gt;And he&amp;#8217;s getting the predictable responses from the static-typing advocates.&lt;/p&gt;
&lt;p&gt;One quibble I&amp;#8217;ve got with what I&amp;#8217;ve just quoted, is that, in many languages, static types aren&amp;#8217;t just comments.  In the C++ implementations I&amp;#8217;ve looked at, for example, the typing is used to generate runtime code which relies on the typing done at compile time.  The other day I wrote about instance variable access in Java, Smalltalk and Ruby.  In C++ instance variable (field) access is static and based on offsets.  If a type error get&amp;#8217;s past the compiler, say by a bad pointer manipulation, the run-time code can access a non-existant variable, possibly clobbering something vital.&lt;/p&gt;
&lt;p&gt;In fact this &amp;#8220;bug&amp;#8221; is possible not in spite of static type checking, but because the compiler uses static type checking to affect the code, and &amp;#8220;throws away&amp;#8221; the information needed at run-time to avoid it.&lt;/p&gt;</description>
      <pubDate>Tue, 12 Feb 2008 09:42:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:aede2c35-fcc8-4e8e-b7a6-a73b53e1441f</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2008/02/12/yegges-at-it-again</link>
      <category>ruby</category>
      <category>types</category>
      <category>statictyping</category>
      <trackback:ping>http://talklikeaduck.denhaven2.com/articles/trackback/489</trackback:ping>
    </item>
    <item>
      <title>Alan Kay on the meaning of OOP</title>
      <description>&lt;img src="http://talklikeaduck.denhaven2.com/files/2008-01-01_alan_kay.jpg" alt="Alan Kay" class="tease-image" width="128px"/&gt;
I've written before in this blog about how the meaning of the term "object-oriented programming" got hijacked from it's original meaning. For example I go into this in some length &lt;a href="http://talklikeaduck.denhaven2.com/articles/2006/07/29/about-me"&gt;in my mini-memoirs.&lt;/a&gt;
&lt;p&gt;I recently ran into an interesting site with links to &lt;a href="http://e7l3.org/classics.html"&gt;"Classical Computer Science Texts"&lt;/a&gt;, which in turn led me to this &lt;a href="http://www.purl.org/stefan_ram/pub/doc_kay_oop_en"&gt;e-mail exchange with Alan Kay on the meaning of OOP from July of 2003.&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This exchange gives support, with details, for my description of Kay's concept of what Object-Oriented Programming was supposed to mean.&lt;/p&gt; &lt;blockquote class="pullquote"&gt;I'm not against types, but I don't know of any type systems that aren't a complete pain, so I still like dynamic typing. &lt;br/&gt;- Alan Kay&lt;/blockquote&gt; 
&lt;p&gt;As Kay explains, the key concepts came from biological cell communications modeled as networked "whole computers" and a desire to "get rid of with data"&lt;/p&gt;
&lt;p&gt;As for the influence of Simula on Smalltalk's notion of classes and inheritance:&lt;/p&gt;
&lt;blockquote&gt;I didn't like the way Simula I or Simula 67 did inheritance (though I thought Nygaard and Dahl were just tremendous thinkers and  designers). So I decided to leave out inheritance as a built-in feature until I understood it better. - Alan Kay&lt;/blockquote&gt;
&lt;p&gt;And summing it up:&lt;/p&gt;
&lt;blockquote&gt;OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things. It can be done in Smalltalk and in LISP. There are possibly other systems in which this is possible, but I'm not aware of them.&lt;/blockquote&gt;
&lt;p&gt;I'd argue that you can do this in Ruby as well. I don't know if Ruby was on Kay's radar in mid-2003.&lt;/p&gt;

</description>
      <pubDate>Tue, 01 Jan 2008 14:39:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:f7442bb9-2a85-4aa9-945b-25d69c9133fb</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2008/01/01/alan-kay-on-the-meaning-of-oop</link>
      <category>ruby</category>
      <category>smalltalk</category>
      <category>types</category>
      <category>alankay</category>
      <trackback:ping>http://talklikeaduck.denhaven2.com/articles/trackback/484</trackback:ping>
    </item>
    <item>
      <title>Perspective on the Static vs. Dynamic Type Debate.</title>
      <description>My &lt;span class="caps"&gt;RSS&lt;/span&gt; reader uncovered a thought provoking paper by Chris Smith entitled &lt;a href="http://cdsmith.twu.net/types.html"&gt;&amp;#8220;What To Know Before Debating Type Systems&amp;#8221;&lt;/a&gt;. It clears up a lot of the confusion around the differences between static and dynamic type systems.
&lt;/p&gt;Much of the debate comes from people with limited exposure to both static and dynamic typing.  He observes that:
&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;Many programmers have used very poor statically typed languages.&lt;/li&gt;
&lt;li&gt;Many programmers have used dynamically typed languages very poorly.&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;
&lt;blockquote class = "pullquote"&gt;
Many programmers have used very poor statically typed languages.
Many programmers have used dynamically typed languages very poorly.
&lt;/ol&gt;
&lt;/blockquote&gt;
&lt;p&gt;He then points out that although there is a good deal of overlap in the problems which can be solved by the two approaches, they are fundamentally different techniques. According to the paper:
&lt;blockquote&gt;
A static type system is a mechanism by which a compiler examines source code and assigns labels (called &amp;#8220;types&amp;#8221;) to pieces of the syntax, and then uses them to infer something about the program&amp;#8217;s behavior.  A dynamic type system is a mechanism by which a compiler generates code to keep track of the sort of data (called its &amp;#8220;type&amp;#8221;) used by the program.  The use of the same word &amp;#8220;type&amp;#8221; in each of these two systems is, of course, not entirely coincidental; yet it is best understood as having a sort of historical or generalized significance.  Great confusion results from trying to find a world view in which &amp;#8220;type&amp;#8221; really means the same thing in both systems.  It doesn&amp;#8217;t.
&lt;/blockquote&gt;
&lt;p&gt;Most of the popular languages with static type systems use static types to mechanically prove that a program is type-safe.  Programs for which this can be proven are accepted, and others are rejected.  The paper shows that for any such type-system there will be correct programs which cannot be proven correct and will be rejected.&lt;p&gt;
&lt;p&gt;Languages with dynamic type systems allow execution of programs without prior proof of &amp;#8216;innocence.&amp;#8217; However, the presence of meta-information at run-time allows for fast-failure and better context for correction when errors do occur.&lt;/p&gt;
&lt;p&gt;The type systems of most popular statically typed languages are rather limited in the properties which can be proven.  Not all program bugs can be statically determined. Seemingly easy checks such as arrray index checking are actually quite difficult to express statically, most languages don&amp;#8217;t check this statically.  As Smith says, &amp;#8220;this is an unusually hard problem to solve without making the languages that solve it unusable, but it&amp;#8217;s a popular one to work on&amp;#8221; as a research topic. Reading this paper has motivated me to learn more about Haskell to explore how easy or hard it is to push the notions of what can be statically checked.&lt;/p&gt;
&lt;p&gt;It occurs to me that a language like C++ has a Napoleonic-code where you are &amp;#8216;guilty until proven innocent&amp;#8217;, but once you&amp;#8217;ve gotten through the trial, you are free to go where you want and do what you want to do. However this means that if you are guilty of something which the type-system doesn&amp;#8217;t discover, like an array bounds violation, your malfeasance might be undetected for some time, and then require sophisticated forensics to debug.  In Ruby or Smalltalk, you are &amp;#8216;innocent until proven guilty,&amp;#8217; but your actions are under a higher level of surveillance by the system.&lt;/p&gt;
&lt;p&gt;Smith wraps up by re-framing the static vs. dynamic typing debate:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Static type systems prove properties of code, but it almost appears that Rice&amp;#8217;s Theorem means we can&amp;#8217;t prove anything of interest with a computer.  If true, that would be an ironclad argument against static typing systems.  Of course, it&amp;#8217;s not true.  However, it is very nearly true.  What Rice&amp;#8217;s Theorem says is that we can&amp;#8217;t determine anything.  (Often the word &amp;#8220;decide&amp;#8221; is used; they mean the same thing here.)  It didn&amp;#8217;t say we can&amp;#8217;t prove anything.  It&amp;#8217;s an important distinction!&lt;/p&gt;
&lt;p&gt;What this distinction means is that a static type system is a conservative estimate.  If it accepts a program, then we know the program has the properties proven by that type checker.  If it fails&amp;#8230; then we don&amp;#8217;t know anything.  Possibly the program doesn&amp;#8217;t have that property, or possibly the type checker just doesn&amp;#8217;t know how to prove.  Furthermore, there is an ironclad mathematical proof that a type checker of any interest at all is always conservative.  Building a type checker that doesn&amp;#8217;t reject any correct programs isn&amp;#8217;t a challenger that may be solved some day; it&amp;#8217;s impossible.&lt;/p&gt;
&lt;p&gt;That, then, is the trade-off.  We get assurance that the program is correct (in the properties checked by this type checker), but in turn we must reject some interesting programs.  To continue the pattern, this is the diametric opposite of testing.  With testing, we are assured that we&amp;#8217;ll never fail a correct program.  The trade-off is that for any program with an infinite number of possible inputs (in other words, any interesting program), a test suite may still accept programs that are not correct &amp;#8211; even in just those properties that are tested.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;He then poses eight questions to ponder when considering where and how to use static or dynamic typing in our programming tasks:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;For what interesting properties of programs can we build static type systems?&lt;/li&gt;
&lt;li&gt;How close can we bring those type systems to the unattainable ideal of never rejecting a correct program?&lt;/li&gt;
&lt;li&gt;How easy can it be made to program in a language with such a static type system?&lt;/li&gt;
&lt;li&gt;What is the cost associated with accidentally rejecting a correct computer program?&lt;/li&gt;
&lt;li&gt;For what interesting properties of programs can we build test suites via dynamic typing?&lt;/li&gt;
&lt;li&gt;How close can we bring those test suites to the unattainable ideal of never accepting a broken program?&lt;/li&gt;
&lt;li&gt;How easy can it be made to write and execute test suites for programs?&lt;/li&gt;
&lt;li&gt;What is the cost associated with accidentally accepting an incorrect computer program?&lt;/li&gt;
&lt;/ol&gt;</description>
      <pubDate>Fri, 29 Jun 2007 08:49:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:dae611e6-76e9-45bd-907a-518303b125ee</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2007/06/29/perspective-on-the-static-vs-dynamic-type-debate</link>
      <category>best_practices</category>
      <category>types</category>
      <category>dynamictypes</category>
      <category>statictypes</category>
      <trackback:ping>http://talklikeaduck.denhaven2.com/articles/trackback/437</trackback:ping>
    </item>
    <item>
      <title>Our Kind of Ducks, Odd Ducks, and Trained Ducks</title>
      <description>&lt;p&gt;A couple of days ago, someone asked on ruby-talk how to implement a cyclic version of Enumerable#each_cons.  In words, he was looking for a method:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;class Array
   def each_cycle(size, start=0)
      #insert implementation here
   end
end

# So that this:
%{a b c d e}.each_cycle(2) {|cyc| p cyc}

#would print:
[&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;]
[&amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]
[&amp;quot;c&amp;quot;, &amp;quot;d&amp;quot;]
[&amp;quot;d&amp;quot;, &amp;quot;e&amp;quot;]
[&amp;quot;e&amp;quot;, &amp;quot;a&amp;quot;]&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As usual, several helpful rubyists responded with suggestions, one of which leads me to &lt;span class="caps"&gt;YADTBE&lt;/span&gt; (Yet Another Duck Typing Blog Entry)&lt;/p&gt;

&lt;p&gt;Here&amp;#8217;s one suggested solution:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;module Enumerable
  def each_cycle(window, start=0)
    (start...length+start).each do |i|
      yield((i...i+window).map {|n| self[n % length]})
    end
  end
end&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Nice solution, but as soon as I saw it, I saw a problem.&lt;/p&gt;
&lt;p&gt;While this will work if the method were defined in the Array class, it doesn&amp;#8217;t work in general for Enumerables. Let&amp;#8217;s write a Kernel method to try some different Enumerables.
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;def try(enum)
  puts &amp;quot;Trying this #{enum.class} ==&amp;gt; #{enum.inspect}&amp;quot;
  begin
    enum.each_cycle(2) {|cyc| p cyc}
  rescue =&amp;gt; ex
    puts ex
  end
  puts
end&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Is it a duck?&lt;/h2&gt;
&lt;p&gt;Okay, now that we&amp;#8217;ve done that, let&amp;#8217;s try some enumerables:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;try(%w{and a one and a two})

# Results in
Trying this Array ==&amp;gt; [&amp;quot;and&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;one&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;two&amp;quot;]
[&amp;quot;and&amp;quot;, &amp;quot;a&amp;quot;]
[&amp;quot;a&amp;quot;, &amp;quot;one&amp;quot;]
[&amp;quot;one&amp;quot;, &amp;quot;and&amp;quot;]
[&amp;quot;and&amp;quot;, &amp;quot;a&amp;quot;]
[&amp;quot;a&amp;quot;, &amp;quot;two&amp;quot;]
[&amp;quot;two&amp;quot;, &amp;quot;and&amp;quot;]&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So this works if the receiver is an Array.&lt;/p&gt;
&lt;h2&gt;Not the kind of duck we&amp;#8217;re looking for&lt;/h2&gt;
&lt;p&gt;Ranges are Enumerable, so let&amp;#8217;s try one.&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;try(1..5)
# Results in
Trying this Range ==&amp;gt; 1..5
undefined local variable or method `length' for 1..5:Range&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Aye, there&amp;#8217;s the rub, the each_cycle method assumes that the receiver implements both a length method, and a [] method.&lt;/p&gt;
&lt;p&gt;This is the kind of requirement which leads some &amp;#8220;strong ducktypers&amp;#8221; to use a respond_to? test.  But does that really work?&lt;/p&gt;
&lt;h2&gt;Odd ducks&lt;/h2&gt;
&lt;p&gt;Well, Hash implements both of those methods, so it would pass the &amp;#8216;responds_to?&amp;#8217; test. Lets try one:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;try(Hash[*(%w{zero, one, two, three}.zip((0..3).to_a)).flatten])
# Which prints:
Trying this Hash ==&amp;gt; {&amp;quot;two,&amp;quot;=&amp;gt;2, &amp;quot;three&amp;quot;=&amp;gt;3, &amp;quot;one,&amp;quot;=&amp;gt;1, &amp;quot;zero,&amp;quot;=&amp;gt;0}
[nil, nil]
[nil, nil]
[nil, nil]
[nil, nil]&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Hmmm, not exactly what we are looking for.  So it&amp;#8217;s not enough to just be an enum with those two methods.  We&amp;#8217;ve just exposed another requirement of Enumeration#each_cycle, that the receiver be indexed by integers.  We&amp;#8217;re getting all nils because all of those nicely generated indices don&amp;#8217;t have corresponding values in the hash, and hash is lenient about indices (i.e. keys) which aren&amp;#8217;t keys.&lt;/p&gt;
&lt;p&gt;So what happens if we try a hash which &lt;strong&gt;does&lt;/strong&gt; have integer keys:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;try(Hash[*((1..3).to_a.zip(%w{one, two, three}).flatten)])
#now we get
Trying this Hash ==&amp;gt; {1=&amp;gt;&amp;quot;one,&amp;quot;, 2=&amp;gt;&amp;quot;two,&amp;quot;, 3=&amp;gt;&amp;quot;three&amp;quot;}
[nil, &amp;quot;one,&amp;quot;]
[&amp;quot;one,&amp;quot;, &amp;quot;two,&amp;quot;]
[&amp;quot;two,&amp;quot;, nil]&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, that sort of works, but it exposes yet another requirement, that the indices of the enum be in the range (0&amp;#8230;enum.size).&lt;/p&gt; 
&lt;h2&gt;I dub thee Sir Duck&lt;/h2&gt;
&lt;p&gt;If we use a slightly different hash:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;try(Hash[*((0..3).to_a.zip(%w{zero, one, two, three}).flatten)])
# We get
Trying this Hash ==&amp;gt; {0=&amp;gt;&amp;quot;zero,&amp;quot;, 1=&amp;gt;&amp;quot;one,&amp;quot;, 2=&amp;gt;&amp;quot;two,&amp;quot;, 3=&amp;gt;&amp;quot;three&amp;quot;}
[&amp;quot;zero,&amp;quot;, &amp;quot;one,&amp;quot;]
[&amp;quot;one,&amp;quot;, &amp;quot;two,&amp;quot;]
[&amp;quot;two,&amp;quot;, &amp;quot;three&amp;quot;]
[&amp;quot;three&amp;quot;, &amp;quot;zero,&amp;quot;]&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So for a hash which looks enough like an Array, the code in Enum actually works.&lt;/p&gt;
&lt;h2&gt;Teaching a range to be our kind of Duck&lt;/h2&gt;
&lt;p&gt;One final example.  Let&amp;#8217;s revisit the case of a range.  We can easily train the range class, or using a singleton class an individual instance of range how to support the each_cycle method:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;class Range
  def length
    (last - first) + (exclude_end? ? 0 : 1)
  end

  def [](i)
    first + i
  end
end

# Now if we try this again:
try(1..5)

# We get:
Trying this Range ==&amp;gt; 1..5
[1, 2]
[2, 3]
[3, 4]
[4, 5]
[5, 1]&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Okay Rick, what&amp;#8217;s your point?&lt;/h2&gt;
&lt;p&gt;Although this exercise might seem a little contrived, it points out that in a dynamically &amp;#8216;typed&amp;#8217; language like Ruby, whether or not a particular object is suitable for a particular use can depend not only on it&amp;#8217;s class, or which modules are mixed in, or whether or not it responds to a particular set of messages, but on &lt;strong&gt;how&lt;/strong&gt; it responds to those messages.&lt;/p&gt;
&lt;p&gt;Using my metaphor of casting for a role in a play, finding the right actor isn&amp;#8217;t just a matter of finding someone who went to a particular acting school, (i.e. has (had) the right class or classes), but it&amp;#8217;s a matter of fitting the individual to the requirements.  It&amp;#8217;s akin to being interviewed for a job, for the employer doing a successful &amp;#8216;type-check&amp;#8217; is a subtle process, and often leads to a probation period (i.e. testing).&lt;/p&gt;
&lt;p&gt;Strong-typing advocates will no doubt see this as a flaw in dynamic systems.  I see it as a strength.  If you think about it honestly, there are a wide variety of requirements which aren&amp;#8217;t expressed in strongly typed language, either because the type system doesn&amp;#8217;t choose to express subsets of types, such as positive numbers, prime numbers, even numbers, etc.; or because state-oriented requirements such as an open file, don&amp;#8217;t really fit into the notion of a static type.&lt;/p&gt;
&lt;p&gt;In my experience, I&amp;#8217;ve gotten to the point where I rarely make the kind of errors which are caught by static types, and the flexibility of dynamic languages far outweighs the relatively minor benefit of early static type checking.  I&amp;#8217;d rather rely on a growing set of test cases to fight bugs.&lt;/p&gt;</description>
      <pubDate>Mon, 30 Apr 2007 16:01:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:56f0fa9a-c597-404b-ab22-5d5da4d93ffe</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2007/04/30/our-kind-of-ducks-odd-ducks-and-trained-ducks</link>
      <category>ruby</category>
      <category>ducks</category>
      <category>types</category>
    </item>
    <item>
      <title>Hungarian Ducks</title>
      <description>&lt;p&gt;It&amp;#8217;s interesting how events flow sometimes.&lt;/p&gt;
&lt;p&gt;One of my major interests outside of software development is human
spaceflight.  Today I opened the local paper to the coming events
section and discovered that the latest &amp;#8216;rich cosmonaut&amp;#8217; to buy a trip
up to the international space station, was arriving there today, and
that his name was Charles Simonyi. As I&amp;#8217;m writing this, he has just
become the first Hungarian in space, or at least that&amp;#8217;s what I heard
him say over &lt;span class="caps"&gt;NASA TV&lt;/span&gt;.&lt;/p&gt;&lt;p&gt;Update: Actually that honor went to &lt;a href="http://en.wikipedia.org/wiki/Bertalan_Farkas"&gt;Bertalan Farkas&lt;/a&gt; who flew on Soyuz 36 in May of 1980.&lt;/p&gt;
&lt;p&gt;Now that&amp;#8217;s a familiar name.  So I spent a little time this morning
looking at his article on Wikipedia, and following some links.
Thereby hangs an interesting tale which relates to duck-typing.&lt;/p&gt;

&lt;h2&gt;Charles Simonyi&lt;/h2&gt;&lt;p&gt;Simonyi was the software architect at the Microsoft Applications
Division back in the early days of Excel and Word.  Before coming to
Microsoft he was at Xerox Parc, at the same time as folks like Alan
Kay, Butler Lampson, Robert Metcalfe, and Dan Ingalls. One of his most
famous inventions was so-called Hungarian Notation, a way of encoding
&amp;#8216;type&amp;#8217; information into the names of variables. This became very
popular among C programmers following the Microsoft programming
bibles.&lt;/p&gt;
&lt;p&gt;Now I never much cared for Hungarian Notation (which I&amp;#8217;ll refer to
simply as Hungarian for the rest of this article).  As normally
practiced it led to variable names which looked like they&amp;#8217;d grabbed
onto part of the C compiler&amp;#8217;s symbol table entry. The variables would
look like iX, and iY (for two different integer variables), or something like pudwGorp for a
pointer to an unsigned doubleword.&lt;/p&gt;
&lt;p&gt;Eventually Hungarian fell out of favor as more and more programmers
realized that it didn&amp;#8217;t contribute enough to pull its weight in ugly
names.&lt;/p&gt;
&lt;h2&gt;Hungarian Dialects&lt;/h2&gt;
&lt;p&gt;What I learned today was that this form of Hungarian is actually a corruption of Simonyi&amp;#8217;s
original Hungarian which, instead of encoding the machine
representation of the variable, encodes the &lt;strong&gt;intentional
use&lt;/strong&gt; of the variable. The Hungarian which most people
saw was that used by the Microsoft Systems Division.  Those in the
know refer to this as Systems Hungarian to distinguish it from
Simonyi&amp;#8217;s original notation which is known by insiders as Apps Hungarian.&lt;/p&gt;
&lt;p&gt;Here, from &lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs600/html/hunganotat.asp"&gt;Simonyi&amp;#8217;s original explanation of Hungarian Notation&lt;/a&gt; is how
variables (or as he called them quantities) should be named:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Quantities are named by their type possibly followed by a
qualifier. A convenient (and legal) punctuation is recommended to
separate the type and qualifier part of a name. (In C, we use a
capital initial for the qualifier as in rowFirst: row is the type;
First is the qualifier.)&lt;/li&gt;
&lt;li&gt;Qualifiers distinguish quantities that are of the same type and
that exist within the same naming context. Note that contexts may
include the whole system, a block, a procedure, or a data structure
(for fields), depending on the programming environment. If one of the
&amp;#8220;standard qualifiers&amp;#8221; is applicable, it should be used. Otherwise, the
programmer can choose the qualifier. The choice should be simple to
make, because the qualifier needs to be unique only within the type
and within the scope&#8212;a set that is expected to be small in most cases.
In rare instances more than one qualifier may appear in a name.
Standard qualifiers and their associated semantics are listed below.
An example is worthwhile: rowLast is a type row value; that is, the
last element in an interval. The definition of Last states that the
interval is &amp;#8220;closed&amp;#8221;; that is, a loop through the interval should
include rowLast as its last value.&lt;/li&gt;
&lt;li&gt;Simple types are named by short tags that are chosen by the
programmer. The recommendation that the tags be small is startling to
many programmers. The essential reason for short tags is to make the
implementation of rule 4 realistic. Other reasons are listed
below.&lt;/li&gt;
&lt;li&gt;Names of constructed types should be constructed from the names of
the constituent types. A number of standard schemes for constructing
pointer, array, and different types exist. Other constructions may be
defined as required. For example, the prefix p is used to construct
pointers. prowLast is then the name of a particular pointer to a row
type value that defines the end of a closed interval. The standard
type constructions are also listed below.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;State Your Intentions&lt;/h2&gt;
&lt;p&gt;Note that Simonyi here is really talking about the intentional
&lt;strong&gt;use&lt;/strong&gt; of the variable rather than it&amp;#8217;s representation
as a bag of bits.&lt;/p&gt;
&lt;p&gt;While this isn&amp;#8217;t really a duck-typing system, it relates in that it
describes the usage of a variable in priority to the machine
representation.  Since the host language here is C, the notation still
has to talk about things like pointers, and whether or not the pointer
is being used as an array.&lt;/p&gt;
&lt;p&gt;So in Apps Hungarian instead of iX, and iY, those variables would
likely just be x and y if they were being used for x and y positions, or row and col for spreadsheet coordinates.
Other names might be wrx for an x position relative to the window
coordinate system, or wrxWidget1 for a particular window relative x
coordinate.&lt;/p&gt;
&lt;p&gt;And the two dialects are incompatible.  As &lt;a href="http://blogs.msdn.com/rick_schaut/archive/2004/02/14/73108.aspx"&gt;explained by Rick Schaut:&lt;/a&gt;&lt;/p&gt;
&lt;blockquote&gt;
For those of us who have grown accustomed to Simonyi-style HN, the whole &#8220;i&#8221; means &#8220;int&#8221; prefix thing is doubly horrible, because the &#8220;i&#8221; prefix in Simonyi-style HN is commonly understood to be an index into an array. If I put a character into an int, I&#8217;m not going to call it iCh. I&#8217;m just going to call it ch. To me, an ich is an index to a character in an array of characters.
&lt;/blockquote&gt;
&lt;p&gt;In comparison to System Hungarian, this seems to be much more useful information than a shorthand encoding of the underlying C datatype.&lt;/p&gt;
&lt;p&gt;This schism between the two dialects came about because Simonyi
used the word &lt;strong&gt;type&lt;/strong&gt; where a better word such as
&lt;strong&gt;usage&lt;/strong&gt; or &lt;strong&gt;role&lt;/strong&gt; would have better
represented the idea.  Perhaps this came from the fact that Simonyi&amp;#8217;s
native language was, not surprisingly, Hungarian rather than
English.  It was actually the technical writers in the Systems
Division who created Systems Hungarian by editing earlier
documentation of the concept without fully understanding it.&lt;/p&gt;
&lt;p&gt;It seems to me that this is the same confusion which confounds so
many discussions of duck-typing in dynamic languages.&lt;/p&gt;
&lt;h2&gt;Further Reading&lt;/h2&gt;
&lt;p&gt;&lt;a href="http://www.byteshift.de/msg/hungarian-notation-doug-klunder"&gt;Another
Microsoft document on Hungarian naming conventions&lt;/a&gt; by Doug
Klunder.&lt;/p&gt;
&lt;p&gt;More explanations of the  &lt;a href="http://blogs.msdn.com/larryosterman/archive/2004/06/22/162629.aspx#163721"&gt;difference between App Hungarian and
System Hungarian have been given byLarry
Osterman&lt;/a&gt; and &lt;a href="http://blogs.msdn.com/rick_schaut/archive/2004/02/14/73108.aspx"&gt;Rick
Schaut&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.joelonsoftware.com/articles/Wrong.html"&gt;Joel
Spoelsky&lt;/a&gt; also wrote about this.  Although I often disagree with
Joel on specific topics, he makes some interesting points.  One thing
that struck me in this article was his use of Hungarian prefixes &amp;#8217;s&amp;#8217;,
and &amp;#8216;us&amp;#8217; to represent safe and unsafe variables in a web application
to indicate which variables should be distrusted and escaped before
being used to render html.  On the surface this seems like a nice idea
if applied consistently, although I&amp;#8217;d still want to make use of
mechanisms like Ruby&amp;#8217;s unsafe tainting of objects to give real
safety.&lt;/p&gt;
&lt;p&gt;You see I just don&amp;#8217;t think that source-code time checking is powerful
enough to trust it completely&lt;/p&gt;</description>
      <pubDate>Mon, 09 Apr 2007 17:07:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:561cd646-bba9-4b54-9b8e-3c6cd76c2d9b</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2007/04/09/hungarian-ducks</link>
      <category>ruby</category>
      <category>ducks</category>
      <category>types</category>
    </item>
    <item>
      <title>Schr&#246;dinger's Duck</title>
      <description>&lt;p&gt;Once again there&amp;#8217;s been a recent furore about the meaning of Ducktyping on the ruby-lang mailing list.&lt;/p&gt;


	&lt;p&gt;A nice young man from Sweden, by the name of Ola Bini, announced &amp;#8220;ductator&amp;#8221; a ruby-gem which allows type validation of
ruby variables.  Now I think that Ola is young based on the photo on his blog site, and I &lt;b&gt;know&lt;/b&gt; that he&amp;#8217;s nice because only
nice people inhabit ruby-lang.&lt;/p&gt;


	&lt;p&gt;The real question is just what kind of a type system ductator represents.  I&amp;#8217;ve taken a brief look at it and it seems to allow one
to describe, via a hash, a list of rules which are  applied to a variable&amp;#8217;s value at run-time. These rules are things like
a list of methods the value must have, a class which the value must be an instance of, or an instance of a subclass of etc.&lt;/p&gt;


&lt;h2&gt;The Cat is Dead, Long Live the Cat&lt;/h2&gt;
In 1935 Erwin Schr&#246;dinger, proposed his famous thought experiment, in which a cat, a a radioactive atomic nucleus, and a
cannister of poison gas rigged ot open if the nucleus decayedare placed in a sealed box. The half-life of the neucleus is
such that there is a probability of 1/2 of the nucleus decaying within one hour.  Because quantum mechanics states that
until a decay is observed, the nucleus really exists as a superposition of decayed and undecayed states, that the cat is
both dead and alive until the box is open and the observation is made.

	&lt;p&gt;Schr&#246;dinger&amp;#8217;s experiment is really a dramatized version of a similar thought experiment previously published in a paper
co-written by Einstein.  Although Einstein intended it as a refutation of the probabilistic nature of quantum mechanics,
the stochastic basis of quantum mechanics has held up over the ensuing seven decades, and such paradoxes have held up.&lt;/p&gt;


	&lt;p&gt;My point here is that, in a sense, the kind of type checker represented by ducktator.  It attempts to observe certain
aspects of an object, and in so doing, has a good chance of killing the duck.&lt;/p&gt;


	&lt;p&gt;Besides the questions of what duck-typing &lt;b&gt;is&lt;/b&gt; or &lt;b&gt;is not&lt;/b&gt;, such a set of checks fails under many common usages in ruby.&lt;/p&gt;


&lt;h2&gt;False Negatives&lt;/h2&gt;
Relying on &lt;b&gt;responds_to?&lt;/b&gt; fails under commonly used Ruby metaprogramming techniques.

	&lt;p&gt;It&amp;#8217;s very common these days to use &lt;b&gt;method_missing&lt;/b&gt; to provide pseudo-methods, or to
create methods on the fly.  ActiveRecord and other RubyOnRails componentry makes extensive use
of this technique.&lt;/p&gt;


	&lt;p&gt;The problem is, of course, that an instance of a class which uses these techniques effectively
gives an invalid response to &lt;b&gt;responds_to?&lt;/b&gt;, or may reply false, &lt;i&gt;until&lt;/i&gt; the missing
method is invoked, creating the method.&lt;/p&gt;


	&lt;p&gt;Poking around a bit, I notice that some of the standard library classes, and others liek ActiveRecord, go to some lengths to make responds_to? track with &lt;i&gt;phantom&lt;/i&gt; methods implemented via &lt;b&gt;:method_missing&lt;/b&gt;, although fixing other reflection methods like &lt;b&gt;:methods&lt;/b&gt; and the class method &lt;b&gt;:instance_methods&lt;/b&gt; doesn&amp;#8217;t seem to be commonly done.
While this is good, it can greatly increase the expense of &lt;b&gt;:responds_to?&lt;/b&gt;. ActiveRecord::Base, aliases &lt;b&gt;:responds_to?&lt;/b&gt; as &lt;b&gt;responds_to_without_attributes?&lt;/b&gt; before redefining &lt;b&gt;:responds_to?&lt;/b&gt; for this reason.&lt;/p&gt;


	&lt;p&gt;In the case of Drb::DrbObject, which is used as the proxy object in Distributed Ruby, handling &lt;b&gt;:respond_to?&lt;/b&gt; on behalf of a remote object, requires marshalling a &lt;b&gt;:respond_to?&lt;/b&gt; message to the remote object, sending the marshalled message, then reading and unmarshalling the reply.  This makes sending &lt;b&gt;:respond_to?&lt;/b&gt; to a remote object &lt;b&gt;much&lt;/b&gt; more expensive.&lt;/p&gt;


And irony of ironies, ductator uses &lt;b&gt;:method_missing&lt;/b&gt; to supply &lt;i&gt;phantom&lt;/i&gt; methods in &lt;b&gt;it&amp;#8217;s&lt;/b&gt;
implementation, without implementing a compensating &lt;b&gt;:respond_to?&lt;/b&gt;!
&lt;h2&gt;False Positives&lt;/h2&gt;
Even if one accepts the philosopy posited by ductator, using it can get tricky. For example, one might check to see if an object &lt;b&gt;responds_to?(:each)&lt;/b&gt;, but exactly what &lt;b&gt;:each&lt;/b&gt; does and how it interacts with it&amp;#8217;s block argument, depends on the class itself. For example, most enumerables yield a single value to the block in &lt;b&gt;:each&lt;/b&gt;, but Hash yields both a key and a value.
The block can be written to accomodate this, but it doesn&amp;#8217;t have to be.  It&amp;#8217;s not clear to me how
to either express or check the type of such block arguments.  I don&amp;#8217;t see a mechanism in
ductator even to check the arity of a proc, and although this could be added, it&amp;#8217;s not really
sufficient since it doesn&amp;#8217;t express the types required of block arguments.
&lt;h2&gt;What&amp;#8217;s the Value?&lt;/h2&gt;
Some folks might find something like ductator to their taste, since it give the impression of making Ruby more like Java or C++. The problem is that in so doing, it makes Ruby less like Ruby.

This kind of type checking seems to be an expensive add-on to the real checking which happens
during program execution.  The fact that everything is an object in ruby means that the really
nasty errors in a language like C when you use an integer as a pointer don&amp;#8217;t happen.  It seems
to me that the best that something like ductator can do is to make a more &amp;#8216;meaningful&amp;#8217; error
description when something goes wrong, but doing that correctly is harder than it first appears,
and in most cases could be accomplished more efficiently by catching errors with rescue clauses
and changing the error reporting, after the fact, which puts the cost of the error on the
offender rather than taxing everyone with run-time pre-flight tests.
&lt;h2&gt;Wrapping up for now&lt;/h2&gt;
The French word for &amp;#8220;duck&amp;#8221; is &amp;#8220;canard.&amp;#8221; 

	&lt;p&gt;In computer jargon, the word &amp;#8220;canard&amp;#8221; means a &lt;a href="http://en.wikipedia.org/wiki/Canard_(computing)"&gt;&amp;#8220;mistaken and confused belief&amp;#8221;&lt;/a&gt;. In my humble opinion, the view of duck typing taken by ducktator and its ilk is in this sense really &amp;#8220;canard&amp;#8221; typing.&lt;/p&gt;


	&lt;p&gt;Le canard est mort, vive long le canard!&lt;/p&gt;


	&lt;p&gt;Or translated into English:&lt;/p&gt;


	&lt;p&gt;The canard is dead, long live the duck!&lt;/p&gt;</description>
      <pubDate>Thu, 21 Sep 2006 11:27:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:77fd8187-b151-4ee5-a7a3-d08e3c414c42</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2006/09/21/schr%C3%B6dingers-duck</link>
      <category>ruby</category>
      <category>ducks</category>
      <category>types</category>
      <trackback:ping>http://talklikeaduck.denhaven2.com/articles/trackback/32</trackback:ping>
    </item>
    <item>
      <title>Ducks Can Be Subtle Birds</title>
      <description>&lt;p&gt;One theory I&amp;#8217;ve seen defines a &amp;#8220;duck type&amp;#8221; as a set of messages which an object bound to a parameter or value needs to understand.  This leads some, who want to make type-checking happen a bit earler, to propose testing the values of such variables with oneor more respond_to? tests before using the object &amp;#8220;in anger.&amp;#8221;&lt;/p&gt;


	&lt;p&gt;But ducks can be subtle&amp;#8230;&lt;/p&gt;


	&lt;p&gt;Sometimes it&amp;#8217;s not enough just to have a certain set of methods in a duck&amp;#8217;s repetoire.&lt;/p&gt;


Let&amp;#8217;s look at an, admittedly cooked-up, example:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;reverse_lookup&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;value&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;dictionary&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
     &lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;dictionary&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;collect&lt;/span&gt; &lt;span class="punct"&gt;{&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt;&lt;span class="ident"&gt;kv&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt; &lt;span class="ident"&gt;kv&lt;/span&gt;&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="number"&gt;1&lt;/span&gt;&lt;span class="punct"&gt;]&lt;/span&gt; &lt;span class="punct"&gt;==&lt;/span&gt; &lt;span class="ident"&gt;value&lt;/span&gt; &lt;span class="punct"&gt;?&lt;/span&gt; &lt;span class="ident"&gt;kv&lt;/span&gt;&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="number"&gt;0&lt;/span&gt;&lt;span class="punct"&gt;]&lt;/span&gt; &lt;span class="punct"&gt;:&lt;/span&gt; &lt;span class="constant"&gt;nil&lt;/span&gt; &lt;span class="punct"&gt;}).&lt;/span&gt;&lt;span class="ident"&gt;compact&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
The method is intended to return an array containing
keys in a &amp;#8220;Dictionary&amp;#8221; which map to the given value:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;irb(main):001:0&amp;gt; load 'reverse_lookup.rb'
=&amp;gt; true

irb(main):002:0&amp;gt; reverse_lookup(1, { 'a' =&amp;gt; 1, 'b' =&amp;gt; 2, 'c' =&amp;gt; 1})
=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;c&amp;quot;]&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
Now, I said that this was a &amp;#8220;cooked-up&amp;#8221; example to illustrate a point about &amp;#8220;ducks.&amp;#8221; It would probably be more proper to implement a method like keys_for_value in Hash or a module which could be used to extend Hash or other &amp;#8220;dictionary&amp;#8221; classes.

	&lt;p&gt;The point here is that for this implementation of reverse_lookup, the only message sent to dictionary is collect, soaccording to the &amp;#8220;respond_to?&amp;#8221; theory any object which has &amp;#8220;collect&amp;#8221; in it&amp;#8217;s repetoire is the right species of duck for the dictionary parameter.&lt;/p&gt;


So let&amp;#8217;s try another duck:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;irb&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;main&lt;/span&gt;&lt;span class="punct"&gt;):&lt;/span&gt;&lt;span class="number"&gt;003&lt;/span&gt;&lt;span class="punct"&gt;:&lt;/span&gt;&lt;span class="number"&gt;0&lt;/span&gt;&lt;span class="punct"&gt;&amp;gt;&lt;/span&gt; &lt;span class="ident"&gt;reverse_lookup&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="number"&gt;1&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="number"&gt;1&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="number"&gt;2&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="number"&gt;3&lt;/span&gt;&lt;span class="punct"&gt;])&lt;/span&gt;
&lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="number"&gt;0&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="number"&gt;1&lt;/span&gt;&lt;span class="punct"&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Well, it works in the sense that nothing blows up, but the results look strange, where did that 0 come from?&lt;/p&gt;


While you weren&amp;#8217;t looking, I snuck a debug parameter into my reverse_lookup method to give an &amp;#8220;x-ray&amp;#8221; view into what&amp;#8217;s happening.
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;irb&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;main&lt;/span&gt;&lt;span class="punct"&gt;):&lt;/span&gt;&lt;span class="number"&gt;004&lt;/span&gt;&lt;span class="punct"&gt;:&lt;/span&gt;&lt;span class="number"&gt;0&lt;/span&gt;&lt;span class="punct"&gt;&amp;gt;&lt;/span&gt; &lt;span class="ident"&gt;reverse_lookup&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="number"&gt;1&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="number"&gt;1&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="number"&gt;2&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="number"&gt;3&lt;/span&gt;&lt;span class="punct"&gt;],&lt;/span&gt; &lt;span class="constant"&gt;true&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
&lt;span class="ident"&gt;kv&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="number"&gt;1&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;kv&lt;/span&gt;&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="number"&gt;0&lt;/span&gt;&lt;span class="punct"&gt;]=&lt;/span&gt;&lt;span class="number"&gt;1&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;kv&lt;/span&gt;&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="number"&gt;1&lt;/span&gt;&lt;span class="punct"&gt;]=&lt;/span&gt;&lt;span class="number"&gt;0&lt;/span&gt;
&lt;span class="ident"&gt;kv&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="number"&gt;2&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;kv&lt;/span&gt;&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="number"&gt;0&lt;/span&gt;&lt;span class="punct"&gt;]=&lt;/span&gt;&lt;span class="number"&gt;0&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;kv&lt;/span&gt;&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="number"&gt;1&lt;/span&gt;&lt;span class="punct"&gt;]=&lt;/span&gt;&lt;span class="number"&gt;1&lt;/span&gt;
&lt;span class="ident"&gt;kv&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="number"&gt;3&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;kv&lt;/span&gt;&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="number"&gt;0&lt;/span&gt;&lt;span class="punct"&gt;]=&lt;/span&gt;&lt;span class="number"&gt;1&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;kv&lt;/span&gt;&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="number"&gt;1&lt;/span&gt;&lt;span class="punct"&gt;]=&lt;/span&gt;&lt;span class="number"&gt;1&lt;/span&gt;
&lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="number"&gt;0&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="number"&gt;1&lt;/span&gt;&lt;span class="punct"&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
The reason that it works at all with an array of integers is a bit of an anomally due to the fact that Ruby integers define the [] method as a bit reference. When kv is an integer kv&lt;sup&gt;&lt;a href="#fn1"&gt;1&lt;/a&gt;&lt;/sup&gt; and kv&lt;sup&gt;&lt;a href="#fn0"&gt;0&lt;/a&gt;&lt;/sup&gt; are respectively the least significant, and second least significant bits in the binary representation of kv.

	&lt;p&gt;Now let&amp;#8217;s try another array;&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;irb&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;main&lt;/span&gt;&lt;span class="punct"&gt;):&lt;/span&gt;&lt;span class="number"&gt;005&lt;/span&gt;&lt;span class="punct"&gt;:&lt;/span&gt;&lt;span class="number"&gt;0&lt;/span&gt;&lt;span class="punct"&gt;&amp;gt;&lt;/span&gt; &lt;span class="ident"&gt;reverse_lookup&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="number"&gt;1&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="constant"&gt;Array&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="constant"&gt;NilClass&lt;/span&gt;&lt;span class="punct"&gt;])&lt;/span&gt;
&lt;span class="constant"&gt;NoMethodError&lt;/span&gt;&lt;span class="punct"&gt;:&lt;/span&gt; &lt;span class="ident"&gt;undefined&lt;/span&gt; &lt;span class="ident"&gt;method&lt;/span&gt; `&lt;span class="punct"&gt;[]'&lt;/span&gt;&lt;span class="string"&gt; for NilClass:Class
        from ./reverse_lookup.rb:2:in `reverse_lookup&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;
        &lt;span class="ident"&gt;from&lt;/span&gt; &lt;span class="punct"&gt;./&lt;/span&gt;&lt;span class="ident"&gt;reverse_lookup&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;rb&lt;/span&gt;&lt;span class="punct"&gt;:&lt;/span&gt;&lt;span class="number"&gt;2&lt;/span&gt;&lt;span class="symbol"&gt;:in&lt;/span&gt; `&lt;span class="ident"&gt;reverse_lookup&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;
        from (irb):4
        from :0&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
So this doesn&amp;#8217;t work at all.

	&lt;p&gt;To sum this up, &amp;#8220;duck types&amp;#8221; aren&amp;#8217;t just collections of messages, whether or not an object will work in any given role also depends on subtleties of the semantics of theimplementation of the corresponding methods, and can also depend on the state of the object as well.&lt;/p&gt;


	&lt;p&gt;Now this shouldn&amp;#8217;t be considered a weakness of &amp;#8220;duck typing&amp;#8221; compared to &amp;#8220;strong typing,&amp;#8221; or something in-between the two. Strong-typing systems have similar &amp;#8220;flaws&amp;#8221;  for example strongly typing a variable to an integer won&amp;#8217;t protect against division by zerowhcn the variable is used as a divisor, nor will it alone protect against out of bounds errors when it&amp;#8217;s used as an array index.  The errors which aren&amp;#8217;t detected by strong-typing is a much larger set than those which are.  In languages which provide limited run-time checking of code which has passed the strong-typing &amp;#8220;hurdle&amp;#8221; the effects can be disastrous. In my humble opinion, the benefits of dynamic typing far outweigh &amp;#8220;unlearning&amp;#8221; what has been taught by other languages such as C++ and Java.&lt;/p&gt;


	&lt;p&gt;Bjarne Stroustrup used to quip, in panel discussions over strong vs. dynamic typing (i.e C++ vs. Smalltalk), that he&amp;#8217;d hate to be a passenger on a plane which flashed a light labled &amp;#8220;message not understood&amp;#8221; in the cockpit when the pilot tried to lower the landing gear.  I&amp;#8217;d hate even more to be a passenger on a plane whose control software seg-faulted under this condition instead because putting the landing gear down caused a buffer overflow.&lt;/p&gt;


	&lt;p&gt;The real answer to uncovering and routing out errors is testing, testing, and more testing.&lt;/p&gt;</description>
      <pubDate>Thu, 10 Aug 2006 11:09:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:57838eb5-1cde-4cad-8ad9-8aecb88af12c</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2006/08/10/ducks-can-be-subtle-birds</link>
      <category>ruby</category>
      <category>ducks</category>
      <category>types</category>
      <category>ruby</category>
      <trackback:ping>http://talklikeaduck.denhaven2.com/articles/trackback/7</trackback:ping>
    </item>
    <item>
      <title>About me</title>
      <description>&lt;p&gt;For those who don&amp;#8217;t want to read a lot, or dont care too much, here&amp;#8217;s the executive summary:&lt;/p&gt;


&lt;ul&gt;
&lt;li&gt;I&amp;#8217;m a long time object programmer.&lt;/li&gt;
&lt;li&gt;I worked for &lt;span class="caps"&gt;IBM&lt;/span&gt; for 31 years.&lt;/li&gt;
&lt;li&gt;I was one of the early adopters/advocates of Smalltalk in &lt;span class="caps"&gt;IBM&lt;/span&gt;.&lt;/li&gt;
&lt;li&gt;I&amp;#8217;ve done a &lt;b&gt;lot&lt;/b&gt; of Smalltalk.
&lt;li&gt;I&amp;#8217;ve done a lot of standards work.&lt;/li&gt;
&lt;li&gt;I&amp;#8217;ve done a lot of Java.&lt;/li&gt;
&lt;li&gt;I&amp;#8217;m now free of &lt;span class="caps"&gt;IBM&lt;/span&gt; and Java, and I&amp;#8217;m learning to love Ruby.&lt;/li&gt;
&lt;/ul&gt;

	&lt;p&gt;As a result of all of the above I&amp;#8217;ve developed lots of strong opinions, weakly held, which I&amp;#8217;d like to share.&lt;/p&gt;


	&lt;p&gt;Now for those with the stamina, some more details on my personal journey&amp;#8230;&lt;/p&gt;


&lt;h1&gt;&lt;span class="caps"&gt;IBM&lt;/span&gt; Research &amp;#8211; Smalltalk and ClassC&lt;/h1&gt;

	&lt;p&gt;My early &lt;span class="caps"&gt;IBM&lt;/span&gt; career gave me a lot of experience with developing software.  One of the things which struck me was how, despite its name, most software was &lt;i&gt;hard&lt;/i&gt;. Hard to develop in the first place, and harder to change to adapt to new requirements.&lt;/p&gt;


	&lt;p&gt;Like many others, I first became aware of Smalltalk when Byte magazine published a special issue on the langage in 1981. I led a small team at &lt;span class="caps"&gt;IBM&lt;/span&gt; Watson Research which developed a language called ClassC which was inspired by that Byte issue, and added Smalltalk message sending to C, it was similar in concept, but not in syntax to Brad Cox&amp;#8217;s Objective-C.  We found out about Objective-C after we&amp;#8217;d already done our worst. ClassC got some use for internal projects in &lt;span class="caps"&gt;IBM&lt;/span&gt; after I moved to a new &lt;span class="caps"&gt;IBM&lt;/span&gt; development lab in Cary, NC in 1984.&lt;/p&gt;


	&lt;p&gt;While I was working on ClassC, the late David N. Smith was dealing with the lawyers at &lt;span class="caps"&gt;IBM&lt;/span&gt; Research, Xerox, and UC Berkeley to get Smalltalk-80 into research.  Dave and I became good friends, along with other folks in the &lt;span class="caps"&gt;IBM&lt;/span&gt; Research User Interface Institute like Dave&amp;#8217;s &amp;#8220;sidekick,&amp;#8221; the late Jerry Archibald, and John T. Richards, and we formed an OO-Smalltalk cadre within &lt;span class="caps"&gt;IBM&lt;/span&gt;. These three were also key participants in the birth of the &lt;span class="caps"&gt;ACM OOPSLA&lt;/span&gt; conference.  Although I personally missed the first &lt;span class="caps"&gt;OOPSLA&lt;/span&gt;, I was a steady participant for the first decade or so, moderating several panel discussions, participating in workshops, and serving on the commitee for &lt;span class="caps"&gt;OOPSLA&lt;/span&gt; &amp;#8216;93.&lt;/p&gt;


	&lt;p&gt;One of the advantages of being part of the &lt;span class="caps"&gt;OOPSLA&lt;/span&gt; community was being able to forge friendships and working relationships with many very bright people such as Ward Cunningham, Kent Beck, and all four of the &amp;#8220;Gang of Four&amp;#8221;&lt;/p&gt;


&lt;h1&gt;Research to Internal Consultant&lt;/h1&gt;

	&lt;p&gt;After moving to the new lab in Cary, I primarily worked as an inter-divisional consultant, working to drive the requirements of my division, which was writing what was then a new style of applications with graphical direct manipulation user interfaces, into the plans of the Personal Systems division.  This got me into a lot of interesting meetings with not only other &lt;span class="caps"&gt;IBM&lt;/span&gt; folks, but also staff from other companies.  For example, I was present at the shotgun marriage between &lt;span class="caps"&gt;IBM&lt;/span&gt; Boca Raton, &lt;span class="caps"&gt;IBM&lt;/span&gt; Hursley, and Microsoft which led to OS/2.&lt;/p&gt;


	&lt;p&gt;My job had given me the opportunity to visit various companies which &lt;span class="caps"&gt;IBM&lt;/span&gt; was investing in, or considering investing in.  I got to meet people like Jean-Marie Hulot of Expertelligence whose interface builder implemented in Lisp got re-written in Objective-C to become NeXTStep&amp;#8217;s Interface Builder, after he moved to NeXT.  A little later, after &lt;span class="caps"&gt;IBM&lt;/span&gt; had made an investment in NeXT, I got to meet Jean-Marie again along with Steve Jobs.  I got to argue with Tony Williams and Bill Gates and his team over the merits of their approach to the early design of what became &lt;a href="http://en.wikipedia.org/wiki/Component_Object_Model"&gt;&lt;span class="caps"&gt;COM&lt;/span&gt;&lt;/a&gt;, which made the C++ type system a little more dynamic andwith the &lt;span class="caps"&gt;IBM&lt;/span&gt; designers of  &lt;a href="http://en.wikipedia.org/wiki/System_Object_Model"&gt;&lt;span class="caps"&gt;SOM&lt;/span&gt;&lt;/a&gt; which tried to make a statically typed systems more resilient to &amp;#8220;schema&amp;#8221; evolution. Both of these were also (failed) attempts to forge an entente in the &lt;a href=#langWars&gt;&amp;#8220;Language wars&amp;#8221;&lt;/a&gt;&lt;/p&gt;


&lt;h1&gt;Smalltalk and &lt;span class="caps"&gt;IBM&lt;/span&gt;&lt;/h1&gt;

	&lt;p&gt;With the availability of Digitalk&amp;#8217;s SmalltalkV, our little OO cadre started to push for adoption of Smalltalk inside &lt;span class="caps"&gt;IBM&lt;/span&gt;.  One of my biggest contributions was to implement a prototype which extended the Smalltalk/V development environment with a direct-manipulation UI and UI Logic (i.e. controller) tool inspired by the Interface builder, which I dubbed Application Builder.  When SmalltalkV/PM came out, I ported the Application Builder to it.&lt;/p&gt;


	&lt;p&gt;The division&amp;#8217;s mission had been shifting towards building application development tools, so this prototype fit in.  Most of those tools were focused on old big-iron ideas and languages, such as &lt;span class="caps"&gt;ISPF&lt;/span&gt;, and Cobol adapted to personal computers, although targeted for mainframes. I arranged to demonstrate Application Builder to Earl Wheeler, the group executive who oversaw our division and who was in charge of a grand &lt;span class="caps"&gt;IBM&lt;/span&gt; strategy called Systems Application Architecture.  Wheeler seemed quite impressed with the App Builder demo and directed us to turn it into a product, and get Smalltalk to be part of &lt;span class="caps"&gt;SAA&lt;/span&gt;, although he wanted it positioned as a &amp;#8220;generator&amp;#8221; instead of a language for internal &lt;span class="caps"&gt;IBM&lt;/span&gt; political reasons.&lt;/p&gt;


	&lt;p&gt;I took the point on getting Smalltalk into &lt;span class="caps"&gt;IBM&lt;/span&gt; product plans, while others took the prototype and turned it into what became VisualAge.  In my opinion they took the visual programming idea a bit too far, but it did the job of getting Smalltalk out to the world from &lt;span class="caps"&gt;IBM&lt;/span&gt;.  And it resulted in &lt;span class="caps"&gt;IBM&lt;/span&gt; Smalltalk, which was implemented by Object Technology International, a company founded by Dave Thomas, although &lt;b&gt;a different&lt;/b&gt; Dave Thomas than PragDave.  As an aside, what is it about guys named Dave Thomas?  Besides &lt;span class="caps"&gt;OTI&lt;/span&gt;-Dave (who is well-known to the Smalltalk community as Big Dave), and PragDave, we have the Dave Thomas from &lt;span class="caps"&gt;SCTV&lt;/span&gt; and Doug of the McKenzie brothers, and the Dave Thomas who founded Wendy&amp;#8217;s. Two Canadians, and two Americans.&lt;/p&gt;


	&lt;p&gt;I also spent a lot of time talking to &lt;span class="caps"&gt;IBM&lt;/span&gt; customers about how object-oriented approaches, and Smalltalk in particular, could help them solve the problems they were having with software being &lt;i&gt;hard.&lt;/i&gt;&lt;/p&gt;


&lt;h1&gt;Smalltalk Distributed&lt;/h1&gt;
One of my big-idea products was Smalltalk Distributed, a feature for VisualAge/Smalltalk.  I led the development on this one.

	&lt;p&gt;Distributed applications were the rage at the time.  There was another approach to distributing Smalltalk from HP called Distributed Smalltalk, but this was really a Smalltalk implementation of the Object Management Groups &lt;span class="caps"&gt;CORBA&lt;/span&gt;.  Although I had been serving as one of the &lt;span class="caps"&gt;IBM&lt;/span&gt; representatives to &lt;span class="caps"&gt;OMG&lt;/span&gt; (as a Smalltalk expert), I didn&amp;#8217;t find this idea particularly interesting or appealing.  The goal of Smalltalk Distributed was transparent distribution of Smalltalk.  Our approach was to implement a distributed implementation of the Smalltalk object model including object storage and garbage collection, message sending, and distributed threads of execution complete with non-local returns.  This was all done completely in Smalltalk, using lots of tricks in using metaprogramming much like what the wizard Ruby metaprogrammers do.&lt;/p&gt;


	&lt;p&gt;This project was technically interesting, and got some use by customers, but it was most valuable as a learning experience. In retrospect, the transparency was both a neat idea, and a problem.  Putting a black box around a distrubuted system is probably not as good an idea of putting a distributed system together out of black boxes.&lt;/p&gt;


	&lt;p&gt;As a result of the Smalltalk Distributed work, I got to spend a month in Sydney Australia, working with Jeff McAffer of &lt;span class="caps"&gt;OTI&lt;/span&gt;, to re-implement some of the ideas such as the distributed threads in a toolkit for building server applications which he called Surfer (or Server) Smalltalk.  Jeff subsequently became one of the technical leads of the Eclipse project.&lt;/p&gt;


&lt;h1&gt;Smalltalk Standardization&lt;/h1&gt;
Another piece of work I did at &lt;span class="caps"&gt;IBM&lt;/span&gt; was to write a report characterizing the similarities and differences between the various Smalltalk implementations.  At that time these were ParcPlace VisualWorks/Smalltalk-80, Digitalk Smalltalk/V, and &lt;span class="caps"&gt;IBM&lt;/span&gt;/Smalltalk (from &lt;span class="caps"&gt;OTI&lt;/span&gt;).  By focusing on the externals of the classes and taking inheritance out of the picture, I wrote a report called &amp;#8220;Smalltak: A Common Base&amp;#8221; which was in effect a rough draft of a reverse-engineered specification for a common Smalltalk language specification.

	&lt;p&gt;When the various Smalltalk vendors expressed interest in forging a Smalltalk language standard, &lt;span class="caps"&gt;X3J20&lt;/span&gt; was formed to develop an &lt;span class="caps"&gt;ANSI&lt;/span&gt; standard, and the common base document became one of the early inputs.  I became the secretary of &lt;span class="caps"&gt;X3J20&lt;/span&gt;.  The standard was published in January of 1998.&lt;/p&gt;


	&lt;p&gt;&lt;a name="langWars"&gt;&lt;/a&gt;&lt;h1&gt;Language Wars &amp;#8211; Dynamic vs. Static OO&lt;/h1&gt;
Of course back in the days before the turn of the century, there was a great debate raging in the &amp;#8220;object-oriented&amp;#8221; programming community, centered around the value of dynamic languages like Smalltalk vs. static languages like C++.&lt;/p&gt;


	&lt;p&gt;One of the things which always attracted me to Smalltalk was that it placed encapsulation above all else.  As Alan Kay noted in his &lt;a href="http://www.smalltalk.org/smalltalk/TheEarlyHistoryOfSmalltalk_Abstract.html"&gt;memoir about the origins of Smalltalk&lt;/a&gt;, his original conception of object-oriented programming was that software should be composed of objects which were, in effect, little computers themselves, which encapsulated both data and behavior, and hid the implementation of both from  other objects, with objects interacting via sending messages to each other and replying. This uniform object model separates languages like Smalltalk and Ruby from other &amp;#8220;object-oriented&amp;#8221; languages. The various versions of Smalltalk all shared this model, although they varied as to some of the semantics of message sending and reception.&lt;/p&gt;


	&lt;p&gt;The idea of classes and inheritance as a way of factoring &lt;b&gt;implementation&lt;/b&gt; was actually a rather late addition to Smalltalk. Although Kay acknowledges the Simula language, which also lacked classes and inheritance, as one of the influences on his thinking leading up to Smalltalk, it&amp;#8217;s been a popular misconception that the better known Simula-67 was his real influence, when Smalltalk and Simula actually evolved independently.&lt;/p&gt;


	&lt;p&gt;Kay&amp;#8217;s term &amp;#8220;object-oriented&amp;#8221; got hijacked when Peter Wegner published paper entitled &amp;#8220;Dimensions of Object Based Language Design&amp;#8221; at the second &lt;span class="caps"&gt;OOPSLA&lt;/span&gt; conference in which he defined &amp;#8220;object-oriented&amp;#8221; as &amp;#8220;objects + classes + inheritance.&amp;#8221;&lt;/p&gt;


Simula-67 spawned a family of languages which called themselves object-oriented but really used classes and inheritance to create hierarchies of abstract data types. In thes languages which include C++, Oberon, and to a lesser extent, Java became popular bacause they were seen as an easy entree into &amp;#8220;object-orientation&amp;#8221; to programmers who were more comfortable with the notion of programs manipulating data from &amp;#8220;a distance.&amp;#8221;  This led Alan Kay to observe that
&lt;blockquote&gt;It is unfortunate that much of what is called &amp;#8220;object-oriented programming&amp;#8221; today is simply old style programming with fancier constructs. Many programs are loaded with &amp;#8220;assignment-style&amp;#8221; operations now done by more expensive attached procedures.&lt;/blockquote&gt;

	&lt;p&gt;Because of this morphing of the meaning of object-oriented programming I started to use the term &amp;#8220;object programming&amp;#8221; instead.  In 1991, I wrote an &lt;span class="caps"&gt;IBM&lt;/span&gt; Research Report called &lt;a href="/files/TypesFromTheClientsViewpoint.PDF"&gt;&amp;#8220;Types From the Client&amp;#8217;s Viewpoint,&amp;#8221;&lt;/a&gt; which very much relates to duck typing and which got cited in a few places.  It&amp;#8217;s long out of print. So I&amp;#8217;ve scanned a hard copy of a draft, to show my thinking at the time.&lt;/p&gt;


	&lt;p&gt;This debate lives on. It pops up as some of the uneasiness of new Ruby programmers coming from the strong-typing/abstract data type community over thinks like &amp;#8220;duck typing.&amp;#8221;  I plan to explain my thinking about duck typing and other related topics here.&lt;/p&gt;


My point here is, I&amp;#8217;ve got strong opinions about this topic.  While I do believe that the key to wisdom is &lt;a href="http://bobsutton.typepad.com/my_weblog/2006/07/strong_opinions.html"&gt;strong opinions weakly held&lt;/a&gt;, I haven&amp;#8217;t seen any reason, over twenty-something years to let go of &lt;b&gt;this&lt;/b&gt; set of opinions.
&lt;h1&gt;Smalltalk to Java&lt;/h1&gt;
Smalltalk was quite successful with our customers. In some ways it looked like it might  become the Cobol of the 21st Century. 

	&lt;p&gt;Then Java came along.  It gave &lt;i&gt;some&lt;/i&gt; of the benefits of Smalltalk like languages in a package which was appealing to the C++ community, a lot of who were starting to become jaded over how C++  (if they actually used the compiler to do more than compile normal C programs), seemed to make development harder.  It kind of looks like C++, and has a more &amp;#8220;regular&amp;#8221; syntax than Smalltalk, which some found wierd, although to the Smalltalkers it was beautiful in it&amp;#8217;s sparseness and elegance.&lt;/p&gt;


	&lt;p&gt;And &lt;span class="caps"&gt;IBM&lt;/span&gt; made the jump to Java.  Most of us who had been working on Smalltalk were encouraged to start working on Java and Java tools.&lt;/p&gt;


	&lt;p&gt;About this time, I took an assignment to &lt;span class="caps"&gt;OTI&lt;/span&gt;, which had been acquired by &lt;span class="caps"&gt;IBM&lt;/span&gt; Canada as a wholly owned subsidiary.&lt;/p&gt;


	&lt;p&gt;&lt;span class="caps"&gt;OTI&lt;/span&gt; itself was making an investment in Java.  The first step was to turn the &lt;span class="caps"&gt;IBM&lt;/span&gt;/Smalltalk virtual machine into what was called the &lt;span class="caps"&gt;UVM&lt;/span&gt; or Universal Virtual Machine.  This could run both Java and Smalltalk bytecodes.  The Java &amp;#8220;natives,&amp;#8221; the equivalent of extensions in Ruby were written in Smalltalk instead of C. The first VisualAge for Java was written in Smalltalk on this &lt;span class="caps"&gt;UVM&lt;/span&gt;.&lt;/p&gt;


	&lt;p&gt;Eventually, as Java evolved this approach became less tenable.  So &lt;span class="caps"&gt;OTI&lt;/span&gt; started building a new &lt;span class="caps"&gt;IDE&lt;/span&gt; in Java.  Our first use for this was for a system to develop embedded Java applications. &lt;span class="caps"&gt;IBM&lt;/span&gt; was getting very interested in embedded software, which they called &amp;#8220;Pervasive Computing&amp;#8221; and had formed a division called PvC to pursue it.   Embedded applications (e.g. code inside an oscilloscope, or a cell phone) written in Smalltalk was an early focus of &lt;span class="caps"&gt;OTI&lt;/span&gt; predating the relationship with &lt;span class="caps"&gt;IBM&lt;/span&gt;.  Java, which was actually first developed within Sun for such embedded applications, was a natural language to employ.&lt;/p&gt;


	&lt;p&gt;The resulting development was called VisualAge/Micro Environment or &lt;span class="caps"&gt;VAME&lt;/span&gt;.  The &lt;span class="caps"&gt;IDE&lt;/span&gt; and UI design was done at the &lt;span class="caps"&gt;OTI&lt;/span&gt; Zurich lab by a team led by Erich Gamma of the &amp;#8220;Gang of Four.&amp;#8221;  This code was almost completely reworked and combined with Jeff McAffer&amp;#8217;s component run-time architecture to become the basis for Eclipse.&lt;/p&gt;


	&lt;p&gt;After working on &lt;span class="caps"&gt;VAME&lt;/span&gt;, I moved over to a group working with customers on embedded Java applications. We majored on automotive applications, since there were a lot of car companies and suppliers who were interested in telematics systems with sophisticated software.  As a &amp;#8220;standards guy&amp;#8221; I represented &lt;span class="caps"&gt;IBM&lt;/span&gt; in standards organizations such as the &amp;#8220;Vehicle Expert Group&amp;#8221; within &lt;a href="http://www.osgi.org/"&gt;OSGi&lt;/a&gt;&lt;/p&gt;


&lt;h1&gt;Back to the Mother Ship for a while&lt;/h1&gt;

	&lt;p&gt;By this time, &lt;span class="caps"&gt;OTI&lt;/span&gt; was being gradually assimilated into the Borg of &lt;span class="caps"&gt;IBM&lt;/span&gt;.  My group was first. Although we continued to work out of the &lt;span class="caps"&gt;OTI&lt;/span&gt; lab in Raleigh, NC, we now reported to &lt;span class="caps"&gt;IBM&lt;/span&gt; PvC management in Austin Tx.&lt;/p&gt;


	&lt;p&gt;Then because I was &amp;#8220;the standards guy,&amp;#8221; I got re-assigned to the PvC architecture department in Austin which was the home of all the PvC standards guys.&lt;/p&gt;


	&lt;p&gt;Eventually, &lt;span class="caps"&gt;OTI&lt;/span&gt; got &amp;#8220;reorganized&amp;#8221; (i.e. broken up) with various pieces going to different &lt;span class="caps"&gt;IBM&lt;/span&gt; divisions.&lt;/p&gt;


&lt;h1&gt;Free at last&lt;/h1&gt;

	&lt;p&gt;After a year or so of this, and 30+ years of &lt;span class="caps"&gt;IBM&lt;/span&gt;, I realized that I was tired of life in a big corporation.  Since I was old enough to still have a traditional pension, it was time to see what life was like on the outside, so I retired in the spring of 2005, and I&amp;#8217;ve been happily pursuing my own muse, happily learning and working on new projects that I wanted to work on.&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;ve recently discovered Ruby, and as an old Smalltalker, I&amp;#8217;m excited to see what looks like the reincarnation of an old friend.  I&amp;#8217;m starting this blog to see what I can share with the Ruby community based on my experiences.&lt;/p&gt;</description>
      <pubDate>Sat, 29 Jul 2006 09:34:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:87bab668-5946-406b-ac56-cbff37acf5ea</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2006/07/29/about-me</link>
      <category>ruby</category>
      <category>war_stories</category>
      <category>smalltalk</category>
      <category>memoirs</category>
      <category>smalltalk</category>
      <category>types</category>
      <category>oop</category>
      <trackback:ping>http://talklikeaduck.denhaven2.com/articles/trackback/3</trackback:ping>
    </item>
    <item>
      <title>On Hunting Ducks</title>
      <description>&lt;p&gt;Five doctors go duck hunting, they draw lots to determine the order in which they will shoot from the blind.&lt;/p&gt;


	&lt;p&gt;The Psychiatrist gets the first chance.&lt;/p&gt;


	&lt;p&gt;A flock of birds fly over. He looks down at his shotgun and says&lt;/p&gt;


&lt;blockquote&gt;They look like ducks! They sound like ducks!  I wonder how they feel about being ducks?&lt;/blockquote&gt;

	&lt;p&gt;Meanwhile the birds fly by.&lt;/p&gt;


	&lt;p&gt;Next up is the internist:&lt;/p&gt;


&lt;blockquote&gt;They look like ducks! They sound like ducks!  But we can&amp;#8217;t rule out the possibility that they might be geese.&lt;/blockquote&gt;

	&lt;p&gt;Then the general practitioner:&lt;/p&gt;


&lt;blockquote&gt;They look like ducks! They sound like ducks!  What do you guys think?.&lt;/blockquote&gt;

	&lt;p&gt;The surgeon takes his place next.  He confidently feels the trigger of his shotgun with his highly skilled right forefinger. The next flock flys over.&lt;/p&gt;


&lt;blockquote&gt;&lt;b&gt;Bang!  Bang!&lt;/b&gt;&lt;/blockquote&gt;

	&lt;p&gt;And the surgeon turns to the pathologist and says:&lt;/p&gt;


&lt;blockquote&gt;Check them out and see if they are ducks.&lt;/blockquote&gt;

	&lt;p&gt;I&amp;#8217;ve known this joke for quite a few years, but I&amp;#8217;ve never thought about it in relationship to software, until I started playing around with Ruby, and discovered an old friend under a new name, &amp;#8220;Duck Typing.&amp;#8221;&lt;/p&gt;


	&lt;p&gt;Last week, I ran across a thread on the ruby forum called &lt;a href="http://www.ruby-forum.com/topic/74246"&gt;&amp;#8220;I&amp;#8217;ll have the Duck!&amp;#8221;&lt;/a&gt; which quickly got lots of responses, and seems to have ended up with jokes.&lt;/p&gt;


	&lt;p&gt;Most of this thread seems to miss the point of duck typing.&lt;/p&gt;


	&lt;p&gt;Once you&amp;#8217;ve really grokked duck-typing you become like that surgeon.  Your perception of the properties of objects that really matter changes.  And you learn to use the pathologist (testing) as a powerful ally.&lt;/p&gt;


	&lt;p&gt;Many programers coming to ruby from languages like C++ and Java are like the psychiatrist, internist, and general praticioner in the duck hunting story.&lt;/p&gt;


	&lt;p&gt;They want certainty in their typing.  They&amp;#8217;ve developed the expectation that early type-checking by a compiler will keep them from making mistakes.&lt;/p&gt;


The problem with this expectation of the power of early type checking is two-fold.
&lt;ul&gt;
&lt;li&gt;It doesn&amp;#8217;t really work&lt;/li&gt;
&lt;li&gt;It is overly constraining&lt;/li&gt;
&lt;/ul&gt;

	&lt;p&gt;It doesn&amp;#8217;t work because the bugs caused by type-checking errors is a small subset of buges in general. Besides, in a fully object&amp;#8212;oriented language like Ruby or Smalltalk, they are rare (because the methods which actually operates on the data is actually automatically strongly type-bound to the data by virtue of it being a method.  It&amp;#8217;s overly constraining because it unnecessarily couples the implementations of the provider and users of the services provided by an object.&lt;/p&gt;


	&lt;p&gt;Rather than droning on about the failures and contraints of early-type checking, which I suspect I&amp;#8217;ll address more fully here as time goes on. Allow me to introduce the notion of types in an object-oriented languagethat I&amp;#8217;ve come to accept over many years.&lt;/p&gt;


	&lt;p&gt;Let me suggest that in a fully object-oriented language types are not properties of objects or classes, but properties of variables which code using an object uses to refer to those objects.  In saying this I&amp;#8217;m recapping the key idea behind an &lt;span class="caps"&gt;IBM&lt;/span&gt; Technical Report I wrote back in 1991 entitled &amp;#8220;Types fromthe Client&amp;#8217;s Viewpoint&amp;#8221; &lt;a href="/files/TypesFromTheClientsViewpoint.PDF"&gt;Which is available here as a pdf file.&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;&amp;#8220;But wait a minute!&amp;#8221;, you say.  &amp;#8220;Aren&amp;#8217;t types for matching objects with variables, so that programmers won&amp;#8217;t  make mistakes when they manipulate the object?&amp;#8221;  To which I reply, &amp;#8220;That&amp;#8217;s not object-oriented.&amp;#8221;&lt;/p&gt;


	&lt;p&gt;Now of course many will argue with this, but I&amp;#8217;d like to revisit the &lt;b&gt;original&lt;/b&gt; conception of object-orientation, a term which was coined by Alan Kay, the inventor of Smalltalk, a language from which Ruby has borrowed a lot.&lt;/p&gt;


	&lt;p&gt;The key idea behind &amp;#8220;object-orientation&amp;#8221; a la Smalltalk and Ruby is the encapsulation of implementation behind an interface defined by a set of messages.  Kay felt that the traditional way of structuring software as programs which processed data, and the attendant separation of program and data was somthing to be avoided.  His view is that it isbetter to compose software out of components which all look like little computers, which contain and bind the programs and state together. This was a synthesis of ideas which he had seen over many years, although they had never been codified before he did it.&lt;/p&gt;


	&lt;p&gt;For an insight into Kay&amp;#8217;s thinking, have a look at the article he wrote some years ago about &lt;a href="http://www.smalltalk.org/smalltalk/TheEarlyHistoryOfSmalltalk_Abstract.html"&gt;&amp;#8220;The Early History of Smalltalk&amp;#8221;&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;Another way of looking at this idea, is that objects should be like little server machines, the exact set of services available and their implementation can vary from server to server, but clients can still use those services in isolation from those details.&lt;/p&gt;


	&lt;p&gt;In both Ruby and Smalltalk, classes and inheritance are used for &lt;b&gt;implementation&lt;/b&gt; sharing, in order to achieve economies in implementing those little computers (objects). This is not an essential feature in Kay&amp;#8217;s conception of OO. The original versions of Smalltalk had neither classes nor inheritance which were introduced in later versions.&lt;/p&gt;


	&lt;p&gt;In parallel with the evolution of Smalltalk, a family of languages based around the concept of &lt;i&gt;Abstract Data Types&lt;/i&gt; became an important Computer Science  research topic.  Abstract Data Types arranged data types into a hierarchy so as to categorize their use as a way to structure more traditional procedural programs, and they called these abstract &lt;b&gt;data types&lt;/b&gt;, classes, and the relationship between classes inheritance, so subclassing was simply a synonym for sub&lt;b&gt;typing&lt;/b&gt;.&lt;/p&gt;


	&lt;p&gt;Since, by the time Smalltalk had been made widely known by the publication of a special issue of Byte magazine devoted to Smalltalk-80 in 1981, it had added classes and inheritance, most observers mistakenly thought of classes and inheritance as essential features of the language.  One of these was Peter Wegner who wrote an influential paper &lt;a href="http://portal.acm.org/affiliated/citation.cfm?id=38823&amp;#38;dl=ACM&amp;#38;coll=ACM&amp;#38;CFID=15151515&amp;#38;CFTOKEN=6184618"&gt;Dimensions of Object based Language Design&lt;/a&gt; which was published in the proceedings of the 1987 &lt;span class="caps"&gt;OOPSLA&lt;/span&gt; conference.  In this paper, Wegner defined &amp;#8220;object-oriented&amp;#8221; languages as those cobining &amp;#8220;objects+classes+inheritance.&amp;#8221;  What he missed was that in Kay&amp;#8217;s conception of object-orientation classes and inheritance were merely implementation artifacts, and that the encapsulation of implementation within an object was the big idea which separates object-oriented languages from procedural languages, and despite the similarity in terminology, abstract-data-type languages, which presented instances of those data types as things to be manipulated from the outside, were far from object-oriented, at least as Kay conceived it.&lt;/p&gt;


	&lt;p&gt;So to wrap this up for now, duck-typing is a natural consequence of the nature of how objects with strongly encapsulated implementations should interact.  The notion of objects containing the code which manipulates them, and hiding that implementation behind a wall, through which messages are sent, is a paradigm shift with powerful consequences.  Because many so-called &amp;#8220;object oriented&amp;#8221; languages missed this paradigm shift, many of their users are uaware the power of thinking about software design from this new perspective.&lt;/p&gt;


	&lt;p&gt;I hope, that in some small way, this blog will help expose the beauty of this paradigm shift.&lt;/p&gt;</description>
      <pubDate>Wed, 26 Jul 2006 15:39:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:be83a184-9f5b-4ab2-97de-de47263fa986</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2006/07/26/my-favorite-duck-typing-story</link>
      <category>ruby</category>
      <category>smalltalk</category>
      <category>humor</category>
      <category>types</category>
      <category>oop</category>
      <enclosure type="application/pdf" length="6274072" url="http://talklikeaduck.denhaven2.com/files/TypesFromTheClientsViewpoint.PDF"/>
      <trackback:ping>http://talklikeaduck.denhaven2.com/articles/trackback/2</trackback:ping>
    </item>
  </channel>
</rss>
