Any difference between t<'a> and 'a t in F#?

前端 未结 4 1773
挽巷
挽巷 2021-02-19 22:49

Is there any difference in meaning between t<\'a> and \'a t in F#? Can they be used interchangeably even after declaration?

4条回答
  •  萌比男神i
    2021-02-19 23:02

    There is no difference, and yes, they can be used interchangeably even after declaration.

    But do note the F# Component Design Guidelines recommendation (Section 4.2):

    Consider using the prefix syntax for generics (Foo) in preference to postfix syntax (T Foo), with four notable exceptions (list, option, array, ref).

    F# inherits both the postfix ML style of naming generic types, e.g. “int list” as well as the prefix .NET style, e.g. “list”. You should prefer the .NET style, except for four specific types. For F# lists, use the postfix form: “int list” rather than “list”. For options, use the postfix form: “int option” rather than “option”. For arrays, use the syntactic name “int[]” rather than either “int array” or “array”. For refs, use “int ref” rather than “ref” or “Ref”. For all other types, use the prefix form: “HashSet”, “Dictionary”, since this conforms to .NET standards

    Also, you'll get a compiler warning if you use the ML-style generic parameter list notation, e.g. ('a,'b) t vs. t<'a,'b>.

    And while we're at it, note the following recommendation in Section 3.1 of the same guide:

    Do use PascalCase for generic parameter names in public APIs, including for F#-facing libraries. In particular, use names like T, U, T1, T2 for arbitrary generic parameters, and when specific names make sense, then for F#-facing libraries use names like Key, Value, Arg (but not e.g. TKey).

    (though personally I tend to ignore this recommendation for F#-facing public libraries).

提交回复
热议问题