Value objects: roll your own?

Andrea Giammarchi andrea.giammarchi at gmail.com
Sun Jan 12 18:44:55 PST 2014


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 ?

Thanks for clarifications



On Sun, Jan 12, 2014 at 6:42 PM, Andrea Giammarchi <
andrea.giammarchi at gmail.com> wrote:

> not sure I understand that ... what if both point2d and point3d specify
> the `+` ? is that possible ?
>
> talking about
>
> ```javascript
>    function + (a :: point2d, b :: point3d) {
>         return point3d(a.x + b.x, a.y + b.y, b.z);
>     }
> ```
>
> specified differently in both point2d and point3d classes
>
>
>
>
>
> On Sun, Jan 12, 2014 at 6:21 PM, Brendan Eich <brendan at mozilla.com> wrote:
>
>> Andrea Giammarchi wrote:
>>
>>> I had same thoughts on being defined inside the class ... it's quite
>>> common out there but here I see it's very easy to create conflicts between
>>> classes.
>>>
>>> What if a generic point2d + point3d is defined in both point2d class and
>>> point3d one ?
>>>
>>
>> The definitions need to be special forms, associated with one (if unary
>> operator) or two value classes -- if you include number as a value class.
>> Also, the value class declaration being memoized allows cross-frame typeof
>> equivalence for free. I forgot to mention this in my last reply.
>>
>>
>>  IIRC Python just consider it's own representation during one operation,
>>> not associating it with "the caller" of such operation ... actually, I've
>>> always been envious of these:
>>> http://rgruet.free.fr/PQR26/PQR2.6.html#SpecialMethods
>>>
>>
>> Value objects do not use double dispatch for dyadic operators, though.
>>
>> /be
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140112/16073148/attachment-0001.html>


More information about the es-discuss mailing list