As I am learning Haskell, I see that there is a lot of language extensions used in real life code. As a beginner, should I learn to use them, or should I avoid them at all cost? I see that it breaks compatibility with Haskell 98 and limits code to pretty much GHC only. However, if I browse packages on Hackage, I see that most of them are GHC-only anyway.
在我学习Haskell时,我发现在现实代码中使用了很多语言扩展。作为初学者,我应该学会使用它们,还是应该不惜一切代价避免使用它们?我发现它破坏了与Haskell 98的兼容性,并且仅将代码限制在几乎GHC中。但是,如果我在Hackage上浏览软件包,我发现大多数软件包都是GHC的。
So, what is an attitude of community towards using language extensions?
那么,社区对使用语言扩展的态度是什么?
And if use of extensions is OK, how can I distinguish extensions which I can use “safely” (those which are likely to become part of the next Haskell standard) from those which are mostly “experimental”? For example, I suppose that -XDisambiguateRecordFields is nice and useful, but is it likely to be supported in the future?
如果扩展的使用是可以的,我如何区分我可以“安全地”使用的扩展(那些可能成为下一个Haskell标准的一部分的扩展)与那些大多数是“实验性”的扩展?例如,我认为-XDisambiguateRecordFields很好用,但未来很可能会得到支持吗?
4 个解决方案
#1
There are some GHC extensions that are too good to live without. Among my favorites are
有一些GHC扩展太好了,不能没有。我最喜欢的是
- Multiparameter type classes
- Scoped type variables
- Higher-rank types
- Generalized algebraic data types (GADTs)
多参数类型
范围类型变量
广义代数数据类型(GADT)
Of these the really essential one is multiparameter type classes.
其中真正重要的是多参数类型。
Some GHC extensions are very speculative and experimental, and you may want to use with caution. A good way to identify a stable and trusted extension is to see if it is slated for inclusion in Haskell Prime, which is hoped to be the successor the Haskell 98.
一些GHC扩展非常具有推测性和实验性,您可能需要谨慎使用。识别稳定且可信赖的扩展的一种好方法是查看是否将其包含在Haskell Prime中,Haskell Prime希望是Haskell 98的后续版本。
I second Don Stewart's suggestion that every extension should be marked using the LANGUAGE
pragma in the source file. Don't enable extensions using command-line options.
我的第二个Don Stewart的建议是,应该使用源文件中的LANGUAGE编译指示标记每个扩展名。不要使用命令行选项启用扩展。
#2
Yes, use extensions as appropriate.
是的,请根据需要使用扩展名。
But be sure to enable them intentionally -- only when you decide you need them. Do this on a per-module basis via {-# LANGUAGE Rank2Types #-}
(for example).
但一定要有意识地启用它们 - 只有当你决定需要它们时才有意义。通过{ - #LANGUAGE Rank2Types# - }(例如)在每个模块的基础上执行此操作。
#3
Generally speaking people do use GHC extensions quite heavily, because they're so useful and Haskell 98 is quite old. Once there's a more up to date standard people may make more effort to stick to it.
一般来说,人们确实大量使用GHC扩展,因为它们非常有用而且Haskell 98已经很老了。一旦有更新的标准,人们可能会更加努力坚持下去。
You can find the status of proposals for the next standard here.
您可以在此处找到下一个标准的提案状态。
#4
The other answers here are good ones. I would add that GHC extensions are not as future-vulnerable (*) as they might be, because GHC seems to be far and away the most popular Haskell compiler, and I don't see that changing soon.
这里的其他答案都很好。我想补充一点,GHC扩展并不像它们可能的那样具有未来的脆弱性(*),因为GHC似乎是最流行的Haskell编译器,我不认为它会很快发生变化。
(*) as in the opposite of "future-proof"
(*)与“面向未来”相反
#1
There are some GHC extensions that are too good to live without. Among my favorites are
有一些GHC扩展太好了,不能没有。我最喜欢的是
- Multiparameter type classes
- Scoped type variables
- Higher-rank types
- Generalized algebraic data types (GADTs)
多参数类型
范围类型变量
广义代数数据类型(GADT)
Of these the really essential one is multiparameter type classes.
其中真正重要的是多参数类型。
Some GHC extensions are very speculative and experimental, and you may want to use with caution. A good way to identify a stable and trusted extension is to see if it is slated for inclusion in Haskell Prime, which is hoped to be the successor the Haskell 98.
一些GHC扩展非常具有推测性和实验性,您可能需要谨慎使用。识别稳定且可信赖的扩展的一种好方法是查看是否将其包含在Haskell Prime中,Haskell Prime希望是Haskell 98的后续版本。
I second Don Stewart's suggestion that every extension should be marked using the LANGUAGE
pragma in the source file. Don't enable extensions using command-line options.
我的第二个Don Stewart的建议是,应该使用源文件中的LANGUAGE编译指示标记每个扩展名。不要使用命令行选项启用扩展。
#2
Yes, use extensions as appropriate.
是的,请根据需要使用扩展名。
But be sure to enable them intentionally -- only when you decide you need them. Do this on a per-module basis via {-# LANGUAGE Rank2Types #-}
(for example).
但一定要有意识地启用它们 - 只有当你决定需要它们时才有意义。通过{ - #LANGUAGE Rank2Types# - }(例如)在每个模块的基础上执行此操作。
#3
Generally speaking people do use GHC extensions quite heavily, because they're so useful and Haskell 98 is quite old. Once there's a more up to date standard people may make more effort to stick to it.
一般来说,人们确实大量使用GHC扩展,因为它们非常有用而且Haskell 98已经很老了。一旦有更新的标准,人们可能会更加努力坚持下去。
You can find the status of proposals for the next standard here.
您可以在此处找到下一个标准的提案状态。
#4
The other answers here are good ones. I would add that GHC extensions are not as future-vulnerable (*) as they might be, because GHC seems to be far and away the most popular Haskell compiler, and I don't see that changing soon.
这里的其他答案都很好。我想补充一点,GHC扩展并不像它们可能的那样具有未来的脆弱性(*),因为GHC似乎是最流行的Haskell编译器,我不认为它会很快发生变化。
(*) as in the opposite of "future-proof"
(*)与“面向未来”相反