How much sugar do classes need?

Mark Miller erights at
Tue Dec 2 09:34:24 PST 2008

On Mon, Dec 1, 2008 at 12:01 PM, David-Sarah Hopwood
<david.hopwood at> wrote:
> Peter Michaux wrote:
>> What is the motivation for choosing the string "to"?
> It is used in E to introduce a method.
> (It was also used in Logo to introduce a procedure declaration, but that
> may be a coincidence.)

Not a coincidence. E's use of "to" was inspired by Logo's.

> The problem is that functions are mutable, which we are stuck with for
> backward compatibility. But there is no such backward compatibility
> constraint on lambdas, which can and should be immutable. So, given
> Allen Wirfs-Brock's syntax for lambda, we can do something like:
>  var self = {
>    method toString: {|| '<' + self.getX() + ',' + self.getY() + '>'},
>    method getX: {|| x},
>    method getY: {|| y},
>    field pubInstVar: 4,
>    const pubInstConst: -4,
>  };
>  return self;
> where the extensions to the object literal syntax used here are:
>  - a 'field' modifier to declare a property non-[[Configurable]],
>  - a 'const' modifier to declare a property non-[[Configurable]]
>   and non-[[Writable]],
>  - a 'method' modifier to declare a property non-[[Configurable]],
>   non-[[Writable]] and non-[[Enumerable]].
> Note that modifiers on object literal properties do not need to be
> reserved keywords ('get' and 'set' are already in this category).

I like this a lot. However, I think using lambdas directly for methods
has a fatal flaw: wrong "return" binding. As Waldemar pointed out at
Kona, lambdas return their completion value, making them hazardous for
direct use in contexts other than control abstractions. When used as a
method, a lambda written only to bring about side effects may
inadvertently leak a shoulda-been-encapsulated-value to its caller.

If someone writing a lambda is thinking "method", they may very well
write a return, which will actually cause a return from the closest
enclosing function. E has the same two levels of we expect for
Harmony: return binding vs completion value, where the first expands
to the second. But we generally use only the return-binding form for
methods, to avoid this leakage hazard. (As you recall, we learned this
lesson the hard way ;).)

Text by me above is hereby placed in the public domain


More information about the Es-discuss mailing list