开发者

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 isLe开发者_开发百科tter .>> spaces
  |>> Method

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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜