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

move type identifiers from type representatives to members #38

Merged
merged 1 commit into from
Nov 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"overrides": [
{
"files": ["*.md"],
"globals": {"Identity": false, "type": false},
"globals": {"Identity": false, "show": false, "type": false},
"rules": {
"no-extra-semi": ["off"],
"no-unused-vars": ["error", {"varsIgnorePattern": "^(Identity|type)$"}]
Expand Down
54 changes: 15 additions & 39 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@
//.
//. For a type to be compatible with the algorithm:
//.
//. - every member of the type MUST have a `constructor` property
//. pointing to an object known as the _type representative_;
//.
//. - the type representative MUST have a `@@type` property
//. - every member of the type MUST have a `@@type` property
//. (the _type identifier_); and
//.
//. - the type identifier MUST be a string primitive and SHOULD have
Expand All @@ -55,34 +52,6 @@
//. _namespace_ will be `null` and _version_ will be `0`.
//.
//. If the _version_ is not given, it is assumed to be `0`.
//.
//. For example:
//.
//. ```javascript
//. // Identity :: a -> Identity a
//. function Identity(x) {
//. if (!(this instanceof Identity)) return new Identity (x);
//. this.value = x;
//. }
//.
//. Identity['@@type'] = 'my-package/Identity';
//. ```
//.
//. Note that by using a constructor function the `constructor` property is set
//. implicitly for each value created. Constructor functions are convenient for
//. this reason, but are not required. This definition is also valid:
//.
//. ```javascript
//. // IdentityTypeRep :: TypeRep Identity
//. var IdentityTypeRep = {
//. '@@type': 'my-package/Identity'
//. };
//.
//. // Identity :: a -> Identity a
//. function Identity(x) {
//. return {constructor: IdentityTypeRep, value: x};
//. }
//. ```

(function(f) {

Expand Down Expand Up @@ -124,11 +93,18 @@
//. ```
//.
//. ```javascript
//. > function Identity(x) {
//. . if (!(this instanceof Identity)) return new Identity (x);
//. . this.value = x;
//. > const Identity$prototype = {
//. . '@@type': 'my-package/Identity@1',
//. . '@@show': function() {
//. . return 'Identity (' + show (this.value) + ')';
//. . }
//. . }
//. . Identity['@@type'] = 'my-package/Identity@1';
//.
//. > const Identity = value =>
//. . Object.assign (Object.create (Identity$prototype), {value})
//.
//. > type (Identity (0))
//. 'my-package/Identity@1'
//.
//. > type.parse (type (Identity (0)))
//. {namespace: 'my-package', name: 'Identity', version: 1}
Expand Down Expand Up @@ -156,8 +132,8 @@
return x != null &&
x.constructor != null &&
x.constructor.prototype !== x &&
typeof x.constructor[$$type] === 'string' ?
x.constructor[$$type] :
typeof x[$$type] === 'string' ?
x[$$type] :
(Object.prototype.toString.call (x)).slice ('[object '.length,
-']'.length);
}
Expand All @@ -174,7 +150,7 @@
//. > type.parse ('nonsense!')
//. {namespace: null, name: 'nonsense!', version: 0}
//.
//. > type.parse (Identity['@@type'])
//. > type.parse (type (Identity (0)))
//. {namespace: 'my-package', name: 'Identity', version: 1}
//. ```
type.parse = function parse(s) {
Expand Down
19 changes: 14 additions & 5 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,26 @@ function Identity(x) {
this.value = x;
}

Identity['@@type'] = 'my-package/Identity';
Identity.prototype['@@type'] = 'my-package/Identity';


var MaybeTypeRep = {'@@type': 'my-package/Maybe'};
var maybeTypeIdent = 'my-package/Maybe';

// Nothing :: Maybe a
var Nothing = {constructor: MaybeTypeRep, isNothing: true, isJust: false};
var Nothing = {
'@@type': maybeTypeIdent,
'isNothing': true,
'isJust': false
};

// Just :: a -> Maybe a
function Just(x) {
return {constructor: MaybeTypeRep, isNothing: false, isJust: true, value: x};
return {
'@@type': maybeTypeIdent,
'isNothing': false,
'isJust': true,
'value': x
};
}

function TypeIdentifier(namespace, name, version) {
Expand All @@ -50,7 +59,7 @@ test ('type', function() {
eq (type (Identity.prototype), 'Object');
eq (type (Nothing), 'my-package/Maybe');
eq (type (Just (0)), 'my-package/Maybe');
eq (type (Nothing.constructor), 'Object');
eq (type (Nothing.constructor), 'Function');

eq (type (false), 'Boolean');
eq (type (0), 'Number');
Expand Down