Hacker News new | past | comments | ask | show | jobs | submit login

In C#, a now popular choice is

  record struct Foo(int Value);
In F#, this is instead done with

  // Foo and Bar cannot be assigned to each other
  type Foo = Foo of int
  type Bar = Bar of int

  // Foo and Bar can be assigned to each other
  type Foo = int
  type Bar = int
The first variant uses single-case unions. It's a bit unfortunate that they also default to classes over structs unless you annotate them with [<Struct>], partially fixed by escape analysis though.

The enum trick is the tersest and will also serialize correctly in most cases, but I have never seen anyone use it like that before.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: