Bring Record toward the needs of Syndicate/js Struct

This commit is contained in:
Tony Garnock-Jones 2018-11-12 22:19:37 +00:00
parent f30b2ae175
commit 9762ad4bfb
1 changed files with 40 additions and 0 deletions

View File

@ -303,6 +303,24 @@ class Record {
this.fields = fromJS(fields);
}
equals(other) {
return (other instanceof Record) &&
Immutable.is(this.label, other.label) &&
Immutable.is(this.fields, other.fields);
}
hashCode() {
return Immutable.List([this.label, this.fields]).hashCode();
}
get(index, defaultValue) {
return this.fields.get(index, defaultValue);
}
set(index, newValue) {
return new Record(this.label, this.fields.set(index, newValue));
}
[PreserveOn](encoder) {
if (Immutable.is(encoder.shortForms[0], this.label)) {
encoder.header(2, 0, this.fields.size);
@ -318,11 +336,33 @@ class Record {
}
}
Record.makeConstructor = function (label, fieldNames) {
const arity = fieldNames.length;
const ctor = (...fields) => {
if (fields.length !== arity) {
throw new Error("Record: cannot instantiate " + (label && label.toString()) +
" expecting " + arity + " fields with " + fields.length + " fields");
}
return new Record(label, fields);
};
ctor.meta = label;
ctor.isClassOf = (v) => ((v instanceof Record) && Immutable.is(label, v.label));
return ctor;
};
class Float {
constructor(value) {
this.value = value;
}
equals(other) {
return (other instanceof Float) && (other.value === this.value);
}
hashCode() {
return this.value | 0; // TODO: something better?
}
[PreserveOn](encoder) {
encoder.leadbyte(0, 0, 2);
encoder.makeroom(4);