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

一世执手 提交于 2019-12-05 13:53:12

问题


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


回答1:


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<T>) 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<int>”. You should prefer the .NET style, except for four specific types. For F# lists, use the postfix form: “int list” rather than “list<int>”. For options, use the postfix form: “int option” rather than “option<int>”. For arrays, use the syntactic name “int[]” rather than either “int array” or “array<int>”. For refs, use “int ref” rather than “ref<int>” or “Ref<int>”. For all other types, use the prefix form: “HashSet<int>”, “Dictionary<string,int>”, 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).




回答2:


No difference at all, is not sure this is worth a whole answer! I prefer the former especially when it comes to multiple type parameters (is that possible with the latter?).




回答3:


I think that the 'a t syntax is more idiomatic (it is used in almost all the MSDN examples and emitted by the compiler will generate that syntax for signature files)

There is a similar example for arrays

int[] , int array

The 'a t syntax is also nicer for concrete parameters -

int list, List<int>

so some consistency argues for 'a t




回答4:


I would say the difference is readability. For one parameter the ' syntax isn't overly confusing but when you get a list of them it becomes much easier to read the angle bracket version.



来源:https://stackoverflow.com/questions/10167359/any-difference-between-ta-and-a-t-in-f

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!