Re: Closures versus objects

"Joe Marshall" <[email protected]>
Newsgroups gmane.comp.lang.lightweight
Message-ID <[email protected]>
On 2/16/07, Spencer Schumann <[email protected]> wrote:
>  The method dispatch mechanisms provided by
> objects take namespace control one step further, which is often very
> useful; in Ruby:
>
>  s = "abcd"
>  s.length
>  a = [1, 2, 3]
>  a.length
>
> seems much more elegant to me than Scheme's
>
>  (let ((s "abcd")
>        (ls '(1 2 3)))
>    (string-length s)
>    (length ls))

You're picking on the non-polymorphic primitives.  Suppose there were a
generic function `len' that returned the length of a string, vector,
list, or whatever
it made sense for.  Then you have this:

(len "abc") => 3
(len '(1 2 3)) => 3

etc.

No difference from Ruby except for the syntax.

But Ruby syntax has a major disadvantage!  Suppose you wanted to late-bind the
method name?  In Scheme you'd do this:

(let ((method   (select-method  len first last)))
  (method "abcd"))

but you can't bind the name of a method identifier in Ruby:

  method = select-method (len, first, last)
  s = "abcd"
  s.method

won't work.




-- 
~jrm
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.