Be explicit about conversions fromJS()

This commit is contained in:
Tony Garnock-Jones 2021-02-22 20:00:15 +01:00
parent a69297c3ba
commit 4353d5280e
2 changed files with 12 additions and 15 deletions

View File

@ -25,11 +25,10 @@ export class Dictionary<V, T extends object = DefaultPointer> extends FlexMap<Va
return d; return d;
} }
constructor(items?: readonly [any, V][]); constructor(items?: readonly [Value<T>, V][]);
constructor(items?: Iterable<readonly [any, V]>); constructor(items?: Iterable<readonly [Value<T>, V]>);
constructor(items?: Iterable<readonly [any, V]>) { constructor(items?: Iterable<readonly [Value<T>, V]>) {
const iter = items?.[Symbol.iterator](); super(canonicalString, items);
super(canonicalString, iter === void 0 ? void 0 : _iterMap(iter, ([k,v]) => [fromJS(k), v]));
} }
mapEntries<W, R extends object = DefaultPointer>(f: (entry: [Value<T>, V]) => [Value<R>, W]): Dictionary<W, R> { mapEntries<W, R extends object = DefaultPointer>(f: (entry: [Value<T>, V]) => [Value<R>, W]): Dictionary<W, R> {
@ -84,9 +83,8 @@ export class Set<T extends object = DefaultPointer> extends FlexSet<Value<T>> {
return x?.[DictionaryType] === 'Set'; return x?.[DictionaryType] === 'Set';
} }
constructor(items?: Iterable<any>) { constructor(items?: Iterable<Value<T>>) {
const iter = items?.[Symbol.iterator](); super(canonicalString, items);
super(canonicalString, iter === void 0 ? void 0 : _iterMap<any, Value<T>>(iter, fromJS));
} }
map<R extends object = DefaultPointer>(f: (value: Value<T>) => Value<R>): Set<R> { map<R extends object = DefaultPointer>(f: (value: Value<T>) => Value<R>): Set<R> {

View File

@ -1,14 +1,14 @@
import { Tag } from "./constants"; import { Tag } from "./constants";
import { Encoder } from "./codec"; import { Encoder } from "./codec";
import { PreserveOn } from "./symbols"; import { PreserveOn } from "./symbols";
import { DefaultPointer, fromJS, is, Value } from "./values"; import { DefaultPointer, is, Value } from "./values";
export const IsPreservesRecord = Symbol.for('IsPreservesRecord'); export const IsPreservesRecord = Symbol.for('IsPreservesRecord');
export class Record<T extends object = DefaultPointer> extends Array<Value<T>> { export class Record<T extends object = DefaultPointer> extends Array<Value<T>> {
readonly label: Value<T>; readonly label: Value<T>;
constructor(label: Value<T>, fieldsJS: any[]) { constructor(label: Value<T>, fields: Value<T>[]) {
if (arguments.length === 1) { if (arguments.length === 1) {
// Using things like someRecord.map() involves the runtime // Using things like someRecord.map() involves the runtime
// apparently instantiating instances of this.constructor // apparently instantiating instances of this.constructor
@ -20,8 +20,8 @@ export class Record<T extends object = DefaultPointer> extends Array<Value<T>> {
return; return;
} }
super(fieldsJS.length); super(fields.length);
fieldsJS.forEach((f, i) => this[i] = fromJS(f)); fields.forEach((f, i) => this[i] = f);
this.label = label; this.label = label;
Object.freeze(this); Object.freeze(this);
} }
@ -72,11 +72,10 @@ export class Record<T extends object = DefaultPointer> extends Array<Value<T>> {
} }
static makeConstructor<T extends object = DefaultPointer>(labelSymbolText: string, fieldNames: string[]): RecordConstructor<T> { static makeConstructor<T extends object = DefaultPointer>(labelSymbolText: string, fieldNames: string[]): RecordConstructor<T> {
return Record.makeBasicConstructor(Symbol.for(labelSymbolText), fieldNames); return Record.makeBasicConstructor<T>(Symbol.for(labelSymbolText), fieldNames);
} }
static makeBasicConstructor<T extends object = DefaultPointer>(label0: any, fieldNames: string[]): RecordConstructor<T> { static makeBasicConstructor<T extends object = DefaultPointer>(label: Value<T>, fieldNames: string[]): RecordConstructor<T> {
const label = fromJS<T>(label0);
const arity = fieldNames.length; const arity = fieldNames.length;
const ctor: RecordConstructor<T> = (...fields: any[]): Record<T> => { const ctor: RecordConstructor<T> = (...fields: any[]): Record<T> => {
if (fields.length !== arity) { if (fields.length !== arity) {