devoured elysium
devoured elysium

Reputation: 105177

Higher verbosity when using Record types in F# in comparison with Discriminated Unions

let Method = { Name:string } //oversimplification

let method_parser =
  spaces >>. many1Satisfy isLetter .>> spaces
  |>> (fun name -> { Name=name })

Had I chosen to use a Method discriminated union, instead, and things would be a bit more succinct:

let method_parser =
  spaces >>. many1Satisfy isLetter .>> spaces
  |>> Method

I believe there is no way to avoid this kind of verbosity when using record types in F#. Am I right?

Upvotes: 2

Views: 277

Answers (1)

Tomas Petricek
Tomas Petricek

Reputation: 243096

A record is very similar to a discriminated union with just a single case. In some cases, I would also prefer a union, because it is easier to use. However, records have two main advantages:

  • They give names to the fields, which partly causes the verbosity, but makes code more self-explanatory
    If you have small number of fields, you can use tuple or single-case union.

  • They allow you to use the { info with Name = "Tomas" } syntax for cloning records
    If you don't need this, you can use standard F# classes (which have more .NET feel)

If you want to get the benefits of records, but still use simple syntax for creating then, then you can define a static member for constructing your record:

type Info = 
  { Name : string 
    Count : int }
  static member Create(name:string, count:int) = 
    { Name = name; Count = count }

Then you can write:

// Simple example using the above type:
let res = ("hello", 5) |> Info.Create

// I expect this would work for your type like this:
let method_parser = 
   spaces >>. many1Satisfy isLetter .>> spaces 
   |>> Method.Create

Upvotes: 6

Related Questions