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 { makeNiceMatchFn, symbolElse } from "../lib/structures/enum.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)); // looks almost the same, but no currying, and the ability to have 'else' const description2 = makeNiceMatchFn(variants)({ fever: fever => `Fever (${fever} degrees)`, [symbolElse]: _ => `Something else`, }); assert.strictEqual("Fever (38.5 degrees)", description2(fever)); assert.strictEqual("Something else" , description2(confused)); assert.strictEqual("Something else" , description2(other));