Skip to content

Change data to no longer combine factory and variant declaration #69

Description

@mlhaufe

data currently defines a class hierarchy as well as a factory for constructing instances of that hierarchy:

const colorData = data({ Red: {}, Green: {}, Blue: {} })

const PointData = data({
    Point2: {x: Number, y: Number},
    Point3: {x: Number, y: Number, z: Number}
})

const PeanoData = data(() => ({
    Zero: {},
    Succ: {pred: PeanoData}
}))

const ListData = data((T) => ({
    Nil: {},
    Cons: {head: T, tail: ListData(T) }
}))

This requires extra effort in type checking, complection, and declaration (due to the fixpoint requirement).

If data did not declare a factory then the above forms can be represented as:

const ColorData = data({}),
    Red = data(ColorData, {}),
    Green = data(ColorData, {})

const PointData = data({}),
    Point2 = data(PointData, {x: Number, y: Number}),
    Point3 = data(PointData, {x: Number, y: Number})

const PeanoData = data({}),
    Zero = data(PeanoData, {}),
    Succ = data(PeanoData, {pred: PeanoData})

const ListData = (T) => {
    const ListData = data({ of: T }),
        Nil = data(ListData, {}),
        Cons = data(ListData, { head: T, tail: ListData })
    return List
}

The syntactic burden is comparable to the original forms except for the parameterized recursive form.

For complect to work as desired, each data declaration has a [children] symbol that returns the extensions:

Peano[children] // [Zero, Succ]

Which enables:

const Peano = complect(PeanoData, [...])

This should have the added benefit of making type declarations easier as they are non-trivial in typescript and very challenging to express in jsdoc.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions