-
Notifications
You must be signed in to change notification settings - Fork 41
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
New standard for creating algebraic data types. #55
Comments
The best option may be to define an $ node
> var S = require ('sanctuary')
> S.Nothing
Nothing
> S.Just (['foo', 'bar', 'baz'])
Just (["foo", "bar", "baz"]) |
That doesn't work in the browser. Here's an example. https://jsfiddle.net/3vuoqpma/ As you can see, Sanctuary values don't produce good console logs in Chrome DevTools. On the other hand, using simple constructors as proposed above does produce good console logs. See the following link for a live example. https://jsfiddle.net/bzpvsce5/ So, the advantages of using constructors over
Note that the Fantasy Land specification requires that the https://github.com/fantasyland/fantasy-land#type-representatives However, Sanctuary does allow you to distinguish between Nevertheless, using Finally, I'd like to talk about the utility function that I wrote to reduce verbosity. const data = (type, name, ...keys) => {
const {length} = keys;
function Data(...vals) {
if (this instanceof Data)
for (let i = 0; i < length; i++)
this[keys[i]] = vals[i];
else return new Data(...vals);
}
Object.defineProperties(Data, {
name: { value: name },
length: { value: length }
});
Data.prototype["static-land/canonical"] = type;
return length > 0 ? Data : new Data;
}; This utility function doesn't change the class name displayed in Chrome DevTools. Hence, instead of showing |
This is likely to change. From fantasyland/fantasy-land#315:
|
I'd like to propose a new standard for creating algebraic data types. Consider the following.
The primary advantage of defining algebraic data types like we did above, is good console logs.
Pattern matching is also standardized. You can use pattern matching with built-in types too.
We can also create a utility function which makes defining new data constructors less verbose.
This makes it easy to define new data constructors for algebraic data types.
I would love to hear your thoughts on this, and continue the discussion in #45 here.
The text was updated successfully, but these errors were encountered: