Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Ruby, Smalltalk and Class Variables (patshaughnessy.net)
35 points by lest on Dec 17, 2012 | hide | past | favorite | 14 comments


This is one of the areas that prototype-based languages simplifies although access issues might trouble class lovers. I do miss NewtonScript for some things.

Example: I create an object called Polygon with the intent to use it as a prototype. I assign the variable sides with a value of 10. So, anything with a parent pointer of Polygon gets 10. I then create Triangle from Polygon and assign sides = 3. It doesn't affect Polygon (which has its own variable), but anything that has a parent pointer of Triangles get the 3.


Io (http://iolanguage.org/) shares the same differential inheritance has NewtonScript:

  Polygon  := Object clone do( sides := 10 )
  Triangle := Polygon clone
  
  Triangle sides println    # => 10
  Triangle sides := 3
  Triangle sides println    # => 3
  Polygon sides println     # => 10


Making that simple is not necessarily limited to prototype-based languages. Another (as good as) dead language of Apple's design, Dylan, is class-based, but has a 'each-subclass' modifier that makes a slot magically appear new in each subclass. You would have to design your polygon class for that, though; it is not something that triangle could magically add.


It is too bad someone hasn't gone back a released a retooled, s-expr version of Dylan. It was an interesting language. People should read the manual (infix) just to see how they did macros.


Towards the end of this article there is a reference to the 'metaclasss'. It should probably be noted that the prefered terminology in the Ruby community now is 'singleton class' and not 'metaclass'.

For quite a while there was little agreement on this name because there was no standard method that could be called to return the singleton class and therefore no agreed upon name. The only access was via syntax:

  (class <<object; self; end)
The results of this expression were called metaclass, eigenclass, singleton class, and probably a few more names.

There is now a standard method:

  object.singleton_class
and so, for the most part, the naming confusion is fading away.


Thanks Gary... yea and all of these things more or less mean the same thing.

At a more technical, detailed level, however, the Ruby core team uses both the term "metaclass" (in the same sense Smalltalk uses it - the class of a class), and the term "singleton" (the hidden class of a single object instance). You can see what I mean by looking through the MRI class.c source file.


Yep and that was one of the reasons that the naming confusion persisted for so long.


Also one of the reasons it's a little bit annoying that they picked a term that already had a different meaning not only within the wider context of programming in general, but within their own language.


I'd never read the linked Alan Kay paper about the development of Smalltalk [1]. It's fascinating!

I've been trying for awhile to find readings or videos (or anything, really) about those early days at ARPA and then PARC. Especially the detailed discussion of the smalltalk development. The names and stories are also pretty neat to read, and have already inspired a few ideas personally.

[1] http://www.smalltalk.org/smalltalk/TheEarlyHistoryOfSmalltal...


I agree - it's an amazing narrative of an interesting time. I wonder if contacting Xerox directly would lead to some more historical archives?


This was also discussed five years ago in this great post by Martin Fowler: http://martinfowler.com/bliki/ClassInstanceVariable.html


Yea it's been a source of discussion (and confusion) for years. Thanks for the link!


In the Perl/Moose world, class variables (attributes) are regarded as a broken concept: http://www.perlmonks.org/?node_id=690482

ref: stvn == "Stevan Little" who is the creator of Moose (https://metacpan.org/module/Moose) & and it's MOP underpinnings (https://metacpan.org/module/Class::MOP)


In Kay's Smalltalk, humans never saw code like that in the article: they defined classes, variables and methods through a graphical interface. The verbose syntax was for computers to read, when you saved code on one machine and loaded it on another. Humans only had to write this stuff after Gnu released a text-only interpreter; the subset of Smalltalk syntax that was originally intended for human use is pretty clear.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: