mlhaufe / brevity

Brevity is a library that enables Feature-Oriented Programming (FOP) and solves the expression problem in a manner that makes data and operation declarations trivial to define and compose.
GNU Affero General Public License v3.0
1 stars 0 forks source link

Missing Trait variant gives a poor error #29

Closed mlhaufe closed 1 year ago

mlhaufe commented 1 year ago

Error:

Uncaught Error: Every variant must have a trait defined

Offending code:

const Shape = Data({
    Circle: ["radius"],
    Ellipse: ["r1", "r2"],
    Rectangle: ["side1", "side2"],
    RightTriangle: ["base", "height"],
    Polygon: ["vertices"],
    Square: ["side"]
});

const shapeToSvg = Trait(Shape, {
    Circle: ({ radius }, container) => {
        const [x, y] = transOrigin(container, [radius, radius]);

        return circle({ cx: x, cy: y, r: radius })
    },
    Ellipse: ({ r1, r2 }, container) => {
        const [x, y] = transOrigin(container, [r1, r2]);

        return ellipse({ cx: x, cy: y, rx: r1, ry: r2 })
    },
    Rectangle: ({ side1, side2 }, container) => {
        const s12 = div(side1, 2),
            s22 = div(side2, 2)

        return polygon(
            transVertices(container, [-s12, -s22], [s12, -s22], [s12, s22], [-s12, s22])
        )
    },
    RightTriangle: ({ base, height }, container) => {
        const b2 = div(base, 2),
            h2 = div(height, 2)

        return polygon(transVertices(container, [0, -h2], [b2, h2], [-b2, h2]))
    }
})

A better message would be:

Invalid Trait declaration. Missing definition for 'Polygon'