Haskell 带连字符的数据构造函数名称
Haskell data constructor name with hyphen
我希望有一个名称包含一些连字符的数据构造函数,如下所示:
data D = The-First | The-Second deriving (Ord, Eq, Show, Read)
有人可以帮忙吗?
Haskell 标准不允许您想要的。引用其中的相关部分:
来自here:
There are six kinds of names in Haskell: those for variables and
constructors denote values; those for type variables, type
constructors, and type classes refer to entities related to the type
system; and module names refer to modules. There are two constraints
on naming:
1.Names for variables and type variables are identifiers beginning with lowercase letters or underscore; the other four kinds of names are identifiers beginning with uppercase letters.
- An identifier must not be used as the name of a type constructor and a class in the same scope.
来自 here:
An identifier consists of a letter followed by zero or more letters,
digits, underscores, and single quotes.
总结:习惯CamelCase格式。一段时间后你会开始喜欢它。 :)
好吧,我想如果有人愿意,可以使用模板 Haskell 来接受表达式并生成构造函数名称(我从未使用过它,所以我不知道它是否可以启动在语法的那个点)。然后我不知道如何允许 [The-First] 而不是 [The - First],因为它们解析相同。
但实际上,使用 CamelCase 或 Camel_Case 要好得多,以至于您不想看任何其他疯狂的选择。
我希望有一个名称包含一些连字符的数据构造函数,如下所示:
data D = The-First | The-Second deriving (Ord, Eq, Show, Read)
有人可以帮忙吗?
Haskell 标准不允许您想要的。引用其中的相关部分:
来自here:
There are six kinds of names in Haskell: those for variables and constructors denote values; those for type variables, type constructors, and type classes refer to entities related to the type system; and module names refer to modules. There are two constraints on naming:
1.Names for variables and type variables are identifiers beginning with lowercase letters or underscore; the other four kinds of names are identifiers beginning with uppercase letters.
- An identifier must not be used as the name of a type constructor and a class in the same scope.
来自 here:
An identifier consists of a letter followed by zero or more letters, digits, underscores, and single quotes.
总结:习惯CamelCase格式。一段时间后你会开始喜欢它。 :)
好吧,我想如果有人愿意,可以使用模板 Haskell 来接受表达式并生成构造函数名称(我从未使用过它,所以我不知道它是否可以启动在语法的那个点)。然后我不知道如何允许 [The-First] 而不是 [The - First],因为它们解析相同。
但实际上,使用 CamelCase 或 Camel_Case 要好得多,以至于您不想看任何其他疯狂的选择。