simplify 'enum'

This commit is contained in:
Joeri Exelmans 2025-06-03 10:42:36 +02:00
parent 366b1ec4e0
commit aee8d5b5e1
6 changed files with 113 additions and 59 deletions

46
tests/enum.js Normal file
View file

@ -0,0 +1,46 @@
import { makeTypeConstructor } from "../lib/meta/type_constructor.js";
import { Char, Double, Unit } from "../lib/primitives/primitive_types.js";
import { unit } from "../lib/primitives/unit.js";
import { makeModuleEnum } from "../lib/structures/enum.types.js";
import { newProduct } from "../lib/structures/product.js";
import { lsType } from "../lib/structures/type_constructors.types.js";
import assert from "node:assert";
const variants = [
newProduct("fever")(_ => Double),
newProduct("confused")(_ => Unit),
newProduct("other")(_ => lsType(_ => Char)),
];
const symbolSymptom = "Symptom__c0442736bb6f11ae99a51b14e4db44b4";
const Symptom = makeTypeConstructor(symbolSymptom)(0);
const [
// constructors
[, {i: newFever}],
[, {i: newConfused}],
[, {i: newOther}],
// match function
[, {i: matchSymptom}],
] = makeModuleEnum(Symptom)(variants);
const fever = newFever(38.5);
const confused = newConfused(unit);
const other = newOther("sweating");
console.log("observe the encoding of different variant instances:");
console.log(" ", fever);
console.log(" ", confused);
console.log(" ", other);
const description = symptom => matchSymptom(symptom)
(fever => `Fever (${fever} degrees)`)
(_confused => `Confused`)
(other => `Other: ${other}`);
assert.strictEqual("Fever (38.5 degrees)", description(fever));
assert.strictEqual("Confused" , description(confused));
assert.strictEqual("Other: sweating" , description(other));

View file

@ -5,7 +5,7 @@ import { Bool, Char, Int } from "../lib/primitives/primitive_types.js";
import { makeModuleStruct } from "../lib/structures/struct.types.js";
import { lsType } from "../lib/structures/type_constructors.types.js";
// Nominal type
const symbolPerson = "Person__22a59ca589b4a7efdbe20b52f380e50f";
const Person = makeTypeConstructor(symbolPerson)(0);