Sanctuary: Type representation for generator functions

Created on 5 Apr 2017  路  9Comments  路  Source: sanctuary-js/sanctuary

executing this in the repl

S.prop('a', {a : function*(){return 12}} )

results in

TypeError: Type-variable constraint violation

prop :: Accessible a => String -> a -> b
                                       ^
                                       1

1)  function* (){return 12} ::

Since there is no type of which all the above values are members, the type-variable constraint has been violated.

It used to work before, now it's breaking.
Is there a type representation for generator functions? (is this even the issue? i'm not completely sure)
I'm currently using the latest version of ES6

Most helpful comment

My pleasure! This is my first public contribution on github and i'm glad it's to Sanctuary :)

All 9 comments

The problem is that function*() { return 12; } is not a member of any of the types in S.env.

One solution is to update the definition of $.AnyFunction. The current definition is essentially:

x => Object.prototype.toString.call(x) === '[object Function]'

Generator functions do not satisfy this predicate. We could use this predicate instead:

x => typeof x === 'function'

If you're interested, @arsaniwilliam, you could submit a pull request for sanctuary-def. Specifically, you would need to update AnyFunction.

@davidchambers Thanks for the explanation.
I will submit a pull request soon.

merge request was created
1 problem though.. I added a line in the test to check that generator functions pass as well, but the es3 linter fails to parse the * .. not sure how to get around that - short of not writing a test.

@arsaniwilliam see this SO question

Thank you for the pull request, Arsani! :)

My pleasure! This is my first public contribution on github and i'm glad it's to Sanctuary :)

So here's a thought.. at some point should we make the distinction between "normal" functions and Generator function types?
I think that was the case before 0.12.x

We could define $.AnyGeneratorFunction, but for now I'm happy to let users define this type in their own code. I'm not sure yet whether such a type is useful.

$ npm install [email protected]
$ node --print 'require("sanctuary").prop("a", {a: function*() { return 12; }})'
[Function: a]
Was this page helpful?
0 / 5 - 0 ratings

Related issues

davidchambers picture davidchambers  路  8Comments

blackxored picture blackxored  路  7Comments

blackxored picture blackxored  路  10Comments

zfoxdev picture zfoxdev  路  8Comments

fnune picture fnune  路  3Comments