Value objects: roll your own?

Axel Rauschmayer axel at rauschma.de
Sun Jan 12 03:45:19 PST 2014


> That is out of date compared to
> 
> http://www.slideshare.net/BrendanEich/js-resp

Thanks!

> specifically slide 15:
> 
> http://image.slidesharecdn.com/jsresp-130914140214-phpapp02/95/slide-15-638.jpg?1379296961
> 
> which has:
> 
> value class point2d { // implies typeof “point2d”
>  constructor point2d(x, y) {
>    this.x = +x;
>    this.y = +y;
>    // implicit Object.freeze(this) on return
>  }
>  point2d + number (a, b) {
>    return point2d(a.x + b, a.y + b);
>  }
>  number + point2d (a, b) {
>    return point2d(a + b.x, a + b.y);
>  }
>  point2d + point2d (a, b) {
>    return point2d(a.x + b.x, a.y + b.y);
>  }
>  // more operators, suffix declaration handler, etc.
> }
> 
> The idea is to provide convenient declartive syntax for operator multimethods that dispatch along the lines proposed by Christian Hansen. Some work required to handle subclassing, Christian has been helping, writing it up is on my plate for this month.


Nice! Should the operator (case) definitions really be inside the class? E.g., conceptually, number + point2d does not belong to a single class, it belongs to both. Hence, I’d use something more like a (global) function declaration:

```
    function number + point2d (a, b) {
        return point2d(a + b.x, a + b.y);
    }
```

Or, possibly:

```
    function + (a :: number, b :: point2d) {
        return point2d(a + b.x, a + b.y);
    }
```

Axel

-- 
Dr. Axel Rauschmayer
axel at rauschma.de

home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140112/16be52ef/attachment.html>


More information about the es-discuss mailing list