t′a之间的任何差值都应与t′a之间的差值相同.和"F#中的t"? [英] Any difference between t<'a> and 'a t in F#?
问题描述
F#中的t<'a>
和'a t
的含义是否有区别?即使声明后也可以互换使用吗?
Is there any difference in meaning between t<'a>
and 'a t
in F#? Can they be used interchangeably even after declaration?
推荐答案
没有区别,是的,即使在声明后也可以互换使用.
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):
考虑优先使用泛型(
Foo<T>
)的前缀语法 后缀语法(T Foo
),具有四个显着例外(list
,option
,array
,ref
).
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#继承了后缀ML的两种命名方式
通用类型,例如"int list
"以及前缀.NET样式,例如
"list<int>
".您应该首选.NET样式,但以下四种除外
具体类型.对于F#列表,请使用后缀形式:"int list
"
而不是"list<int>
".对于选项,请使用后缀形式:"int option
"
而不是"option<int>
".对于数组,请使用语法名称
"int[]
"而不是"int array
"或"array<int>
".对于refs
,
使用"int ref
"而不是"ref<int>
"或"Ref<int>
".对于其他所有
类型,请使用前缀形式:"HashSet<int>
","Dictionary<string,int>
",
因为这符合.NET标准
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
此外,如果您使用ML样式的通用参数列表表示法(例如,),则会收到编译器警告. ('a,'b) t
与t<'a,'b>
.
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:
请在公共API中将PascalCase用作通用参数名称, 包括面向F#的库.特别是使用T,U, T1,T2用于任意通用参数,以及使用特定名称时 因此,对于面向F#的库,请使用诸如Key,Value和Arg之类的名称 (但不是TKey).
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).
(尽管就我个人而言,我倾向于忽略面向F#的公共图书馆的建议).
(though personally I tend to ignore this recommendation for F#-facing public libraries).
这篇关于t′a之间的任何差值都应与t′a之间的差值相同.和"F#中的t"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!