Skip to content
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

Inconsistency with CoffeeScript #2

Closed
Artazor opened this issue Oct 8, 2014 · 3 comments
Closed

Inconsistency with CoffeeScript #2

Artazor opened this issue Oct 8, 2014 · 3 comments

Comments

@Artazor
Copy link
Owner

Artazor commented Oct 8, 2014

CoffeeScript's decision to infer generatorness from yield yields to orthogonality violation.
The following js snippet is semantically homogenous and correct:

var so = require('so');
var fs = require('then-fs');

var case1 = so(function*(){
    var value = 'Immediate';
    return value;
});

var case2 = so(function*(){
    var value = yield fs.readFile('somefile.txt', 'utf8');
    return value;
});

var combined = so(function*(){
    var a = yield case1();
    var b = yield case2();
    return {a:a, b:b};
});

var main = so(function*(){
    console.log(yield combined());
});

main().done();

This is achieved because so has signature described as follows:

type PromiseGen a = a -> [Promise] -- consider generators that yields Promises
so::PromiseGen a -> (a -> Promise) -- convert generator to the function that returns a Promise
-- Here monadic nature of Promise is omitted for simplicity

In contrast, the following CoffeScript counterpart will fail without changing the semantics of so()

so = require 'so'
fs = require 'then-fs'

case1 = so -> 'Immediate' # fails since argument is not a GeneratorFunction
case2 = so -> yield fs.readFile 'somefile.txt', 'utf8'

combined = so -> 
    a = yield case1()
    b = yield case2()
    {a, b}

main = so -> console.log yield combined()

main().done();

Generators without yield are strange in the case of generating sequences (i.e. using generators as intended), but are pretty valid corner case for asynchronous flow control based on generators.

So if CoffeeScript will release it's proposed semantics we should consider implementing so() argument check to be a GeneratorFunction and change it's semantics if it is not.

Would appreciate any thoughts from @ForbesLindesay, @jashkenas, @Nami-Doc

@Artazor Artazor changed the title Inconsistent behavior when used in CoffeeScript Inconsistency with CoffeeScript Oct 8, 2014
@vendethiel
Copy link

Repeating what has been said -- yield return is a way to have a yield without it being effective

@Artazor
Copy link
Owner Author

Artazor commented Oct 8, 2014

Thank you, for the reply! Yes yield return is the solution of the problem.

@Artazor Artazor closed this as completed Oct 8, 2014
@Artazor
Copy link
Owner Author

Artazor commented Oct 8, 2014

So the final solution would look like:

so = require 'so'
fs = require 'then-fs'

case1 = so -> yield return 'Immediate' # solution!
case2 = so -> yield fs.readFile 'somefile.txt', 'utf8'

combined = so -> 
    a = yield case1()
    b = yield case2()
    {a, b}

main = so -> console.log yield combined()

main().done();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants