Fable: Records with single field of type unit generate broken constructor

Created on 6 Sep 2018  路  3Comments  路  Source: fable-compiler/Fable

Description

When creating a record of a single element of type unit, the generated class constructor is invalid and produces a ReferenceError at run-time.

Repro code

Fable REPL samples...

type Test = {
    Abc: unit
}

printfn "%A" <| { Abc = () }

produces:

import { setType } from "fable-core/Symbol";
import _Symbol from "fable-core/Symbol";
import { compareRecords, equalsRecords, Unit } from "fable-core/Util";
import { printf, toConsole } from "fable-core/String";
export class Test {
  constructor() {
    this.Abc = abc;
  }

  [_Symbol.reflection]() {
    return {
      type: "Test.Test",
      interfaces: ["FSharpRecord", "System.IEquatable", "System.IComparable"],
      properties: {
        Abc: Unit
      }
    };
  }

  Equals(other) {
    return equalsRecords(this, other);
  }

  CompareTo(other) {
    return compareRecords(this, other) | 0;
  }

}
setType("Test.Test", Test);
toConsole(printf("%A"))(new Test());

Notice that the constructor is missing the abc argument that it uses to initialize the field.

type Test = {
    Abc: unit
    Xyz: int
}

printfn "%A" <| { Abc = (); Xyz = 123 }

produces:

import { setType } from "fable-core/Symbol";
import _Symbol from "fable-core/Symbol";
import { compareRecords, equalsRecords, Unit } from "fable-core/Util";
import { printf, toConsole } from "fable-core/String";
export class Test {
  constructor(abc, xyz) {
    this.Abc = abc;
    this.Xyz = xyz | 0;
  }

  [_Symbol.reflection]() {
    return {
      type: "Test.Test",
      interfaces: ["FSharpRecord", "System.IEquatable", "System.IComparable"],
      properties: {
        Abc: Unit,
        Xyz: "number"
      }
    };
  }

  Equals(other) {
    return equalsRecords(this, other);
  }

  CompareTo(other) {
    return compareRecords(this, other) | 0;
  }

}
setType("Test.Test", Test);
toConsole(printf("%A"))(new Test(null, 123));

So when another field is added to the record, the constructor is properly formed.

Related information

Latest everything (Fable 1), as observed via the REPL as well.

Most helpful comment

Looks OK in Fable2

Here's a Repl of it working.

Console: {Abc = null}

All 3 comments

Looks OK in Fable2

Here's a Repl of it working.

Console: {Abc = null}

Thanks for checking @xdaDaveShaw! Would it be OK to close this issue if it works for you with Fable 2 @SirUppyPancakes?

Yep, definitely!

Was this page helpful?
0 / 5 - 0 ratings