Skip to content

Commit

Permalink
* datatype.js: some design changes
Browse files Browse the repository at this point in the history
  • Loading branch information
swannodette committed May 19, 2010
1 parent f5f72b2 commit e483e6b
Showing 1 changed file with 67 additions and 52 deletions.
119 changes: 67 additions & 52 deletions datatype.js
@@ -1,60 +1,75 @@
(function() {

var multiMap = {};
var multiMap = {};
function Type() {};
function Protocol() {};

_.mixin({
extenderOf: function(x, protocol) {
return _(x.protocols).indexOf(protocol) != -1;
},

Multi: function(name, type, fn) {
if(_.isUndefined(multiMap[name])) {
var dispatchTable = {},
dispatcher = {};
multiMap[name] = dispatchTable;
dispatcher[name] = function(instance) {
var rest = _.rest(arguments);
return multiMap[name][instance.name].apply(instance, rest);
};
_.mixin(dispatcher);
}
multiMap[name][type.name] = fn;
},

_.mixin({
extenderOf: function(x, protocol) {
return _(x.protocols).indexOf(protocol) != -1;
},
isProtocol: function() {
return x instanceof Protocol;
},

multi: function(name, type, fn) {
if(_.isUndefined(multiMap[name])) {
var dispatchTable = {};
multiMap[name] = dispatchTable;
var dispatcher = {};
dispatcher[name] = function(instance) {
var rest = _.rest(arguments);
return multiMap[name][instance.name].apply(instance, rest);
Protocol: function(obj) {
obj.prototype = new Protocol;
obj.toString = function() {
return ["<Protocol: ", (obj.name || _uniqueId("UnnamedProtocol")), ">"].join("");
};
_.mixin(dispatcher);
}
multiMap[name][type.name] = fn;
},
return obj;
},

isType: function(x) {
return x instanceof Type;
},

protocol: function(obj) {
obj.toString = function() {
return ["<Protocol: ", (obj.name || _uniqueId("UnnamedProtocol")), ">"].join("");
};
return obj;
},

datatype: function(obj) {
obj = obj || {};
var klass = function() {
if(_.isFunction(this.initialize)) {
return this.initialize.apply(this, arguments);
} else {
return this;
}
};
var protocols = obj.protocols || [];
var name = obj.name = (obj.name || _.uniqueId("UnnamedType"));
var methodMap = protocols
.map(function(x) { x = _(x).clone(); delete x.name; delete x.toString; return x; });
obj =_.reduce(methodMap.concat(obj), {}, function(memo, m) {
return _.extend(memo, m);
});
obj = _(obj).extend({
toString: function() {
return ["<Type: ", name, ">"].join("");
}
});
klass.name = name;
klass.prototype = obj;
klass.prototype.type = klass;
klass.prototype.protocols = klass.protocols = protocols;
return klass;
}
});
Type: function(obj) {
obj = obj || {};
var klass = function() {
if(_.isFunction(this.initialize)) {
return this.initialize.apply(this, arguments);
} else {
return this;
}
};
var protocols = obj.protocols || [],
name = obj.name = (obj.name || _.uniqueId("UnnamedType")),
methodMap = protocols.map(function(x) {
x = _(x).clone(); delete x.name; delete x.toString; return x;
});
obj =_.reduce(methodMap.concat(obj), {}, function(memo, m) {
return _.extend(memo, m);
});
obj = _(obj).extend({
toString: function() {
return ["<Type: ", name, ">"].join("");
}
});
klass.name = name;
klass.prototype = new Type();
klass.prototype.type = klass;
klass.prototype.protocols = klass.protocols = protocols;
_(obj).each(function(v, k) {
klass.prototype[k] = v;
});
return klass;
}
});

})();

0 comments on commit e483e6b

Please sign in to comment.