mlscript icon indicating copy to clipboard operation
mlscript copied to clipboard

Miscellaneous features and fixes to implement

Open LPTK opened this issue 7 months ago • 0 comments

Syntax

  • [ ] Special-case {} to mean an empty object (currently, one has to use new Object)
  • [ ] Parsing this currently fails:
    foo(1,
      2)
    

Mutability/Immutability

  • [ ] Make arrays and object literals immutable by default (using Object.freeze)
  • [ ] Allow mut modifier on array and object literals This will remove the {} footgun (it resolves to runtime.Unit, which is currently not frozen!). For a fresh mutable records, users will need to write mut {}.
  • [ ] Make class ctors use Object.freeze by default – this will probably require generating getters/setters for mut vals

Bugs

  • [x] Currently, && and || are not short-cirtuiting!

Code-gen improvements

  • [ ] Should avoid generating very repetitive "else throw match error" structures

    Example
    data class Foo(a)
    
    :sjs
    case Foo(Foo(Foo(x))) then x
    //│ JS (unsanitized):
    //│ let lambda;
    //│ lambda = (undefined, function (caseScrut) {
    //│   let param0, param01, param02, x;
    //│   if (caseScrut instanceof Foo1.class) {
    //│     param0 = caseScrut.a;
    //│     if (param0 instanceof Foo1.class) {
    //│       param01 = param0.a;
    //│       if (param01 instanceof Foo1.class) {
    //│         param02 = param01.a;
    //│         x = param02;
    //│         return x
    //│       } else {
    //│         throw new globalThis.Error("match error");
    //│       }
    //│     } else {
    //│       throw new globalThis.Error("match error");
    //│     }
    //│   } else {
    //│     throw new globalThis.Error("match error");
    //│   }
    //│ });
    //│ lambda
    //│ = [function]
    

LPTK avatar Jun 07 '25 09:06 LPTK