Ruby のブロックってオブジェクトじゃないよね。これって“驚き最小の法則”に反しない?


プログラマ向けオンラインマガジンの bitwise magazine にて、どうしてかくも多くの人が Ruby に魅了されるのか…を編集者があれこれ語った記事が掲載されているのですが、ちょっと笑ってしまった一節。

Huw: Matz has said that, with Ruby, he has 'tried to follow the principle of "least surprise."'. I don't know about you, but I am constantly surprised by Ruby. To take a few examples:

  • I am surprised that a block is not an object. If I create a block in Dolphin Smalltalk it is a true object with an independent existence. When I ask it to tell me its class it replies BlockClosure. In Ruby, a block only exists when it is either appended to a method or when you (as Matz says) "objectify" it by wrapping it up in a Proc object. If I ask it to tell me its class it replies "syntax error"!
  • I am surprised that...

というのも、以前、ラリー・ウォールRuby に対するこんな発言があったのを思い出したからです。

たとえば、なんでもオブジェクト扱いするのは、「初心者にとっての驚き最小の原則」の侵害だと思う。初心者にとっては、数字は数字だし、文字列は文字列だ。コンピュータが扱う限りでは、それらは当然オブジェクトなのだろうし、もちろん、上級者は、これらをオブジェクト扱いするのを問題にさえしない。けれど、いきなりオブジェクト指向を強いると、加速中の初心者にとっては徐行帯になってしまう。


今や Perl も Everything is an Object であることを強調する時代。この種の言及はもはや文字通り過去のものだとは思うのですが、まあ、当時としてはこれが普通の感覚だったのでしょう。ともあれ、感想を述べる人のスタンスで、まったく逆の批判にさらされる人気者の Ruby も大変ですね。w


地方から東京に出てきた学生が、地元に電話をかけるときに、電話のこちら側では周りの友人たちに変なイントネーションだと笑われ、電話の向こう側からは東京弁なんかしゃべるなやと嫌みを言われて困惑する、少々気の毒なシチュエーションを、なぜだか、ふと思い出してしまいました。


ところで、Everything is an Object というのは、アラン・ケイが Samlltalk の言語設計において意識したとされる6項目のうちのひとつで、LISP が関数を含めた多くのものを第一級オブジェクトとして扱うことに影響を受けてのこととされています。

  1. Everything is an object
  2. Objects communicate by sending and receiving messages (in terms of objects)
  3. Objects have their own memory (in terms of objects)
  4. Every object is an instance of a class (which must be an object)
  5. The class holds the shared behavior for its instances (in the form of objects in a pogram list)
  6. To eval a program list, control is passed to the first object and the remainder is treated as its message

第一項目に限らず「すべてがオブジェクト」であることが、ことさらに強調されていますね。


ただ、パラダイムとしての“ケイのオブジェクト指向”がどんなものかを考えたとき、この「すべてがオブジェクト」であることよりは、むしろ第二、第六項目としてここで挙げられている“多くのことをメッセージングで行なう(…として記述する)”ことのほうが重要であることは、けっこう見落とされがちです。

The big idea is "messaging" -- that is what the kernal of Smalltalk/Squeak is all about (and it's something that was never quite completed in our Xerox PARC phase). The Japanese have a small word -- ma -- for "that which is in between" -- perhaps the nearest English equivalent is "interstitial". The key in making great and growable systems is much more to design how its modules communicate rather than what their internal properties and behaviors should be. Think of the internet -- to live, it (a) has to allow many different kinds of ideas and realizations that are beyond any single standard and (b) to allow varying degrees of safe interoperability between these ideas.


たしかに「すべてがオブジェクト」であることは目を引く特徴のひとつではあるけれど、けっしてそれは本質なのではなく(少なくとも Smalltalk においては…)、もっと大切な「すべてがメッセージング」であるための副次的な要件にすぎないのではないのかなぁ…と考えます。ちょうどインスタンスベースのオブジェクト指向において、インスタンスの生成がプロトタイピングやコピーによって行なわれること(端的には、クラスがないこと)が本質であるかのように誤解されがちなのと同じように…。


からめて、この問題の「すべてはオブジェクト」に引っ張られて、よく「Smalltalk では if 文もオブジェクトである」というような表現を見かけるのですが、これらについては(意味的にも、少し通じにくいということもあるので…)できるだけ、「Smalltalk では if 文もメッセージングとして表現される」と言い換えられるべきだとも思うしだいです。


関連