Skip to content

Instantly share code, notes, and snippets.

@cmawhorter
Created August 12, 2014 01:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmawhorter/991fa48d53ad551a6a64 to your computer and use it in GitHub Desktop.
Save cmawhorter/991fa48d53ad551a6a64 to your computer and use it in GitHub Desktop.
All the joy of loose typing, combined with a little more strictness
// Playing with overloads and stricter typing in JS -- https://github.com/cmawhorter/tiptop
var fn = require('../lib/tiptop');
function MyObj() {
}
MyObj.prototype.save = fn.overloaded(
function() {
return this.save(id);
},
function(id$Number) {
return this.save(id$Number, name);
},
function(id$Number, name$String) {
return this.save(id$Number, name$String, dt);
},
function(id$Number, name$String, dt$Date) {
return DB_Save({
id: id$Number
, name: name$String
, dt: dt$Date
});
}
);
// or you could also share code. wrapped just because
(function() {
function sharedCode() {
}
MyObj.prototype.blah = fn.overloaded( ... overloads that call sharedCode ... );
})()
var obj = new MyObj();
obj.save();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment