Scala中的类型类的命名约定是什么? [英] What is the naming convention for typeclasses in Scala?
问题描述
Comparable
时,你可以说它的对象是可比的。但是,类型类的命名约定并不完善。例如, Int
有一个 Numeric
隐式可用,所以你可以说 Int 是一个数字
类型。但是然后有类型类型 Ordering
。我不明白为什么选择这个名字。 Int
是 Ordering
type没有任何意义。也许它应该被理解为 Int
type有一个 Ordering
。然后在Scalaz中有 Equal
和 Show
。我完全不知道为什么选择这些名字(除了他们在Haskell中是如此)。我尝试着看着类型类的母语Haskell,这是一个很好的命名约定,但发现它并不存在。 Haskell的人似乎并不关心名称(这是我从邮件列表讨论中收集的)。但来自Java世界,我确实在乎名字。我不太习惯于类型说全部范例。
问题是:您遵循什么命名约定,命名类型类?
解决方案事实上,Haskell中的事情大都是直截了当的:Type类通常基于操作表示,而不是类型参数所代表的内容。
例如:
-
读取,显示
:标准字符串序列化/反序列化函数被定义。
-
Eq
, Ord
:类型类型on其中定义了相等和顺序关系。
-
Enum
:定义后继操作的类型类,其值可以被枚举。
-
Monoid
, Functor
, Monad
:定义操作类型的类ons与相似命名的数学结构相关。
有些例子不太好:例如, Num
是一类类型,其上定义了隐式算术操作的特定集合,但没有理由使用 Num $ c $在任何传统意义上,c>实际上都必须是一个数字。这可能是支持数字操作的类型,假装数字实际上意味着该短语中的任何东西。
总之,数字
可能是模仿的一个坏例子,如果一个类只有一个函数(可能有多个变体),那么在函数后面命名类几乎总是安全的,就像 show
与显示
。
但真的,主要的是根据类型类的函数来思考,而不是类型参数。思考动词,而不是名词。无论如何,名词都是无聊的惰性事物,所以从动作和操作的角度来看,可能会导致更好的程序设计。
In Java world, the naming conventions for interfaces are pretty much well established. For instance, when you say certain class implements the interface Comparable
, you can say that it's objects are comparable. However the naming conventions for typeclasses are not so well established. For example, Int
has a Numeric
implicit available, so you can say "Int
is a Numeric
type". But then there is typeclass Ordering
. I fail to see why this name was chosen. "Int
is an Ordering
type" doesn't make any sense. Perhaps it is supposed to be read as "Int
type has an Ordering
". Then there are Equal
and Show
in Scalaz. I totally have no idea why these names were chosen (apart from that they are so in Haskell.) I tried looking at Haskell, the mother language of typeclasses, for a good naming convention but found that there doesn't really exist any. Haskell guys don't really seem to care about names (That's what I gathered from the mailing list discussions). But coming from Java world, I do care about names. I am not quite able to get accustomed to "types say it all" paradigm.
The question is: What naming conventions do you follow, if at all you do, for naming the typeclasses?
解决方案 Actually, things are mostly straightforward in Haskell: Type classes are typically named based on what the operations represent, not what the type parameter represents.
For example:
Read
, Show
: Classes of types for which standard string serialization/deserialization functions are defined.
Eq
, Ord
: Classes of types on which equality and ordering relations, respectively, are defined.
Enum
: The class of types defining a "successor" operation, i.e., whose values can be enumerated.
Monoid
, Functor
, Monad
: Classes of types which define the operations associated with the similarly-named mathematical structures.
Some examples aren't so good: For instance, Num
is a class of types on which an ad-hoc collection of vaguely-arithmetic-oriented operations are defined, but there's no reason that an instance of Num
must actually be a number in any conventional sense. This could perhaps be handwaved as "types supporting numeric operations", pretending that "numeric" actually means anything in that phrase.
In short, Numeric
is probably a bad example to imitate, and if a type class has only one function (possibly with multiple variants on it), it's almost always safe to name the class after that function, as with show
vs. Show
.
But really, the main thing is to think in terms of the functions on the type class, not the type parameter. Think verbs, not nouns. Nouns are dumb inert things anyway, so thinking in terms of actions and operations is likely to lead to better program design.
这篇关于Scala中的类型类的命名约定是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!