Agoric / layer-cake

Class/traits-like composition of objects-as-closures
Apache License 2.0
8 stars 4 forks source link

Generator-less alternative proposal #3

Closed DavidBruant closed 4 years ago

DavidBruant commented 4 years ago

My understanding of the proposal is that one problem to solve is that each layer must reference the final object before it's fully built The way it's currently solved is to invoke a generator first to generate the layer (at a moment the methods references an unassigned variable, but that's ok, because methods aren't called yet) Then, from the layers, build the final object and the "super layers" and feed them back via a second call to next with the proper arguments

A possible alternative would look like this on the consumer side :

function BasePointLayer(x, y) {
  return self => ({
    getX() { return x; },
    getY() { return y; },
    toString() { return `<${self.getX()},${self.getY()}>`; },
  });
}

function WobblyPointLayer(wobble) {
  return (self, supr) => ({
    getX() { return supr.getX() + wobble++; },
  });
}

function makeWobblyPoint(x, y, wobble) {
  return makeClassCake([BasePointLayer(x, y), WobblyPointLayer(wobble)]);
}

I think this can work with self and suprs being initially blank objects ({}) that will be mutated later on during cake assembly

If further enforcement is necessary, an alternative is to make self and suprs proxies that throw if touched before being finalized and behave as expected when the objects have been finalized

attn @erights @katelynsills