Value objects: roll your own?

Andrea Giammarchi andrea.giammarchi at gmail.com
Sun Jan 12 21:39:11 PST 2014


"the call is ambiguous and an error is thrown" .. that's OK, but this is
assuming you know upfront all libraries in the game, right?

Now, how about modules? Would this approach cause unknown possible
conflicts all over?

I understand the Python approach might not solve this neither, but I am
really wondering about this operators part ... I wasn't even aware of it
and if it works I find it very awesome/interesting!

Thanks, not trying to get any goat though


On Sun, Jan 12, 2014 at 9:21 PM, Brendan Eich <brendan at mozilla.com> wrote:

> We're not abusing guard :: syntax, are you trying to get my goat? :-P
>
>
> Andrea Giammarchi wrote:
>
>> recap ...
>>
>> in class point3d
>> ```javascript
>>    function + (a :: point2d, b :: point3d) {
>>         return point3d(a.x + b.x, a.y + b.y, b.z);
>>     }
>> ```
>>
>> in class point2d
>> ```javascript
>>    function + (a :: point2d, b :: point3d) {
>>         return point2d(a.x + b.x, a.y + b.y); // ignore b.z
>>         // or do something else with z
>>     }
>> ```
>>
>> which one would be the result and why? what if both defines a and b
>> arguments differently too ?
>>
>
> Those are distinct multimethods. One returns a point3d, one a point2d.
> Declaring both as you show (with the bogo-syntax changed to avoid guard-::)
> results in ambiguous method calls on instances: we get the left+ symbol's
> set from point2d.prototype, and the right+ symbol's set from
> point3d.prototype, for an expression of the form
>
>   var presult = p2 + p3.
>
> We interset the sets. The result is not a singleton, but a set with two
> functions. Since there's no return-type dispatch, or indeed any clue what
> type is wanted for presult, the call is ambiguous and an error is thrown.
>
> This points out another benefit of putting multimethod pattern-based
> definitions in value class declarations: we can raise an early error when
> the second value class is processed.
>
> /be
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140112/0164749a/attachment.html>


More information about the es-discuss mailing list