Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Records with single field of type unit generate broken constructor #1561

Closed
SirUppyPancakes opened this issue Sep 6, 2018 · 3 comments
Closed

Comments

@SirUppyPancakes
Copy link

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.

@SirUppyPancakes SirUppyPancakes changed the title Records with single field of type unit generates broken constructor Records with single field of type unit generate broken constructor Sep 6, 2018
@xdaDaveShaw
Copy link
Contributor

Looks OK in Fable2

Here's a Repl of it working.

Console: {Abc = null}

@alfonsogarciacaro
Copy link
Member

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

@SirUppyPancakes
Copy link
Author

Yep, definitely!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants