Bruce Johnston
Bruce Johnston

Reputation: 8634

Which F# type names and declaration syntax are considered idiomatic?

F# has multiple ways to declare the same types. This is likely because of the dual lineage of F# as both a member of the ML family and a .NET language. I haven't been able to find any guidance on which style is more idiomatic.

Specifically, I want to know:

Sources:

Context: I'm working on some API documentation that is explaining how data in a data store maps to .NET types, along with how those types are typically declared in both C# and F#.

Upvotes: 1

Views: 151

Answers (2)

Tarmil
Tarmil

Reputation: 11362

For doubles, it's pretty much always float. Unless you deal with both singles and doubles and need to ensure clarity I guess.

For generic types, the usual syntax I use and see people use is:

  • int option
  • int list
  • int[]
  • For all other types, including F#-specific ones like Async, Set, and Map, angle bracket syntax is used.

The only type that I feel has a significant split is seq (an alias for IEnumerable): I'd say the majority of people use seq<int> but a significant number of people write int seq. Either way, you should definitely use seq and not IEnumerable. Similarly, you should use the alias ResizeArray for System.Collections.Generic.List.

Upvotes: 2

glennsl
glennsl

Reputation: 29106

The F# Core Library reference, which seems like a good example to follow, seems to prefer float, int[] and seq<int>.

Upvotes: 1

Related Questions