Skip to content

Instantly share code, notes, and snippets.

@hsk
Created July 4, 2014 01:32
Show Gist options
  • Save hsk/7c46873e8ac98750babc to your computer and use it in GitHub Desktop.
Save hsk/7c46873e8ac98750babc to your computer and use it in GitHub Desktop.
var traits = {};
function trait(name, methods) {
traits[name] = methods;
}
function struct() {
var args = arguments;
// インターフェイスを作成
var interfaces = function() {};
for (var j = 0; j < args[1].length; j++) {
interfaces[args[1][j]] = {};
}
function find(x) {
for (var y in interfaces) {
if (x in traits[y]) return y;
}
}
for (var x in args[2]) {
// インターフェイスを取得
interfaces[find(x)][x] = args[2][x];
}
return function() {
var i = 0;
for (var x in args[0]) {
this[x] = arguments[i];
i++;
}
for (x in interfaces) {
this[x] = interfaces[x];
}
};
}
trait("Eq1", {
eq1: ["fun", ["self"], "bool"]
});
trait("Eq2", {
eq2: ["fun", ["self"], "bool"]
});
var I = struct({
"x": "int"
}, ["Eq1", "Eq2"], {
"eq1": function(s, a) {
return s.x == a.x;
},
"eq2": function(s, a) {
return s.x == a.x;
}
});
var i = new I(10);
var j = new I(10);
console.log(i.x);
console.log(i.Eq1.eq1(i, j));
console.log(i.Eq2.eq2(i, j));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment