-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow constructors to return new objects. #95
Comments
This also allows |
The existing definition for Promise.all() doesn't permit this, really; we need to reimplement it using _cast. Also, we need to get rid of step 8 in CreatePromiseCapabilityRecord in order to use Promise.all/Promise.race. (See domenic/promises-unwrapping#95). Although we end up needing to reimplement all/race anyway, so perhaps that's moot.
This is bizarre. You should not have to create an entirely new class each instance. Something is wrong here, either in the spec or in your idea of the the implementation. "Defeating the optimization" sounds bad. I will be sure to look into this further. In general constructors should never return anything; there is no point in making spec changes to accomodate such abberant subclasses. But it should of course be possible to build subclasses without returning things from subclass constructors. |
This was the monad-promise I was thinking. No weird return-from-constructors: https://gist.github.com/domenic/263d922475a7d7fa9414 |
I spent a good amount of time playing around with this tonight. You example is slightly broken; I've commented on the gist. The implementation at https://github.com/cscott/prfun/tree/monad-wip-1b actually works (a test suite and everything) but I end up having to reimplement To restate: the desired characteristic is that the type signature of But even though I don't actually need it for Monadic promises, I still think returning a value from the constructor would be a useful and harmless ability to preserve. |
Well, And yes, |
The odd thing is the the problem with But in the end, I don't think the benefit of monadic use is worth the cost of forcing all the 'normal' users of This bug is actually about letting Promise constructors return objects, which I think is worth allowing even though it turns out that it's not key to a monadic Promise implementation. |
The existing definition for `Promise.all`/`Promise.race` doesn't resolve the array elements enough (we ought to `cast` instead of `resolve`), so we need to reimplement them. Haven't finished auditing the code and writing test cases to prove that we unwrap exactly the right number of times in each method. See domenic/promises-unwrapping#95
Independent of whether or not it is a good idea to create a new class per instance as a work around for Promise.all/race behavior, the appropriate way to do it that doesn't run into step 8 iisues is: It seems to me that in a complete ES6 implementation, the appropriate way to define this is using @@create: class MonadicPromise extends Promise {
static [Symbol.create] {
return Object.setProtoypeOf(super(), (class extends this{}).prototype;
// a new MonadicPromise subclass is needed for each instance,
// in order to thwart the optimization in Promise.resolve
// (formerly Promise.cast)
}
constructor(exec) {
super((f,r) => exec( v => f([v]), r));
}
},
then(f, r) {
return super.then(v => f(v[0]), r);
},
// XXX Note that I wouldn't have to override this if it weren't for the
// check in step 8 of CreatePromiseCapabilityRecord.
resolve(v) {
return new MonadicPromise(function(r) { r(v); });
}
}; |
The check in step 8 of
CreatePromiseCapabilityRecord
should be rewritten to:This allows certain subclasses to be more easily written; see https://mail.mozilla.org/pipermail/es-discuss/2014-February/036293.html
The text was updated successfully, but these errors were encountered: