Skip to content

Compile data constructor pattern definitions the same as UCS patterns #521

Description

@LPTK

UCS patterns are compiled fine:

data class Add(val lhs, val rhs)

:sjs
case Add(0, 0) then 1
//│ ————————————| JS (unsanitized) |————————————————————————————————————————————————————————————————————
//│ let lambda;
//│ lambda = (undefined, function (caseScrut) {
//│   let arg$Add$0$, arg$Add$1$;
//│   if (caseScrut instanceof Add1.class) {
//│     arg$Add$0$ = caseScrut.lhs;
//│     arg$Add$1$ = caseScrut.rhs;
//│     if (arg$Add$0$ === 0) {
//│       if (arg$Add$1$ === 0) {
//│         return 1
//│       }
//│       throw globalThis.Object.freeze(new globalThis.Error("match error"));
//│     }
//│     throw globalThis.Object.freeze(new globalThis.Error("match error"));
//│   }
//│   throw globalThis.Object.freeze(new globalThis.Error("match error"));
//│ });
//│ return lambda
//│ —————————————————| Output |—————————————————————————————————————————————————————————————————————————
//│ = fun

But pattern definitiosn generate very suboptimal code:

pattern A = Add(0, 0)

:sjs
case @annotations.compile A then 1
//│ ————————————| JS (unsanitized) |————————————————————————————————————————————————————————————————————
//│ let lambda1;
//│ lambda1 = (undefined, function (caseScrut) {
//│   let inlinedVal, lhs, rhs, result1$, result1$1, tmp, tmp1;
//│   if (caseScrut instanceof Add1.class) {
//│     if (caseScrut instanceof globalThis.Object) {
//│       if ("lhs" in caseScrut) {
//│         let inlinedVal1;
//│         lhs = caseScrut.lhs;
//│         if (lhs === 0) {
//│           inlinedVal1 = true;
//│         } else {
//│           inlinedVal1 = false;
//│         }
//│         tmp = globalThis.Object.freeze({
//│           input: lhs,
//│           result: inlinedVal1
//│         });
//│       } else {
//│         tmp = globalThis.Object.freeze({
//│           input: null,
//│           result: false
//│         });
//│       }
//│     } else {
//│       tmp = globalThis.Object.freeze({
//│         input: null,
//│         result: false
//│       });
//│     }
//│     if (caseScrut instanceof globalThis.Object) {
//│       if ("rhs" in caseScrut) {
//│         let inlinedVal1;
//│         rhs = caseScrut.rhs;
//│         if (rhs === 0) {
//│           inlinedVal1 = true;
//│         } else {
//│           inlinedVal1 = false;
//│         }
//│         tmp1 = globalThis.Object.freeze({
//│           input: rhs,
//│           result: inlinedVal1
//│         });
//│       } else {
//│         tmp1 = globalThis.Object.freeze({
//│           input: null,
//│           result: false
//│         });
//│       }
//│     } else {
//│       tmp1 = globalThis.Object.freeze({
//│         input: null,
//│         result: false
//│       });
//│     }
//│     result1$1 = tmp.result;
//│     if (result1$1 === true) {
//│       result1$ = tmp1.result;
//│       if (result1$ === true) {
//│         inlinedVal = true;
//│       } else {
//│         inlinedVal = false;
//│       }
//│     } else {
//│       inlinedVal = false;
//│     }
//│   } else {
//│     inlinedVal = false;
//│   }
//│   if (inlinedVal === true) {
//│     return 1
//│   }
//│   throw globalThis.Object.freeze(new globalThis.Error("match error"));
//│ });
//│ return lambda1
//│ —————————————————| Output |—————————————————————————————————————————————————————————————————————————
//│ = fun

Metadata

Metadata

Assignees

Type

No fields configured for Task.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions