Skip to content

Commit

Permalink
Merge pull request #38 from sanctuary-js/davidchambers/no-type-reps
Browse files Browse the repository at this point in the history
move type identifiers from type representatives to members
  • Loading branch information
davidchambers authored Nov 3, 2019
2 parents 49f7ff7 + c947f9f commit a87cb8e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 45 deletions.
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

0 comments on commit a87cb8e

Please sign in to comment.