首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >了解newtype以及何时使用它

了解newtype以及何时使用它
EN

Stack Overflow用户
提问于 2021-03-01 09:44:09
回答 1查看 43关注 0票数 0

我对purescript和函数式编程非常陌生

从类型系统的角度来看,新类型是不同的。这额外增加了一层类型安全。

这是我的数独解算器的开头大约50行代码的样子(到目前为止):

代码语言:javascript
复制
newtype Index = Index Int
newtype Option = Option Int
newtype Column = Column Int
newtype Row = Row Int
newtype Box = Box Int
newtype Cell = Cell Int

derive newtype instance eqIndex :: Eq Index
derive newtype instance eqOption :: Eq Option
derive newtype instance eqColumn :: Eq Column
derive newtype instance eqRow :: Eq Row
derive newtype instance eqBox :: Eq Box
derive newtype instance eqCell :: Eq Cell

derive newtype instance semiringIndex :: Semiring Index
derive newtype instance semiringOption :: Semiring Option
derive newtype instance semiringColumn :: Semiring Column
derive newtype instance semiringRow :: Semiring Row
derive newtype instance semiringBox :: Semiring Box
derive newtype instance semiringCell :: Semiring Cell

derive newtype instance ringIndex :: Ring Index
derive newtype instance ringOption :: Ring Option
derive newtype instance ringColumn :: Ring Column
derive newtype instance ringRow :: Ring Row
derive newtype instance ringBox :: Ring Box
derive newtype instance ringCell :: Ring Cell

derive newtype instance commutativeRingIndex :: CommutativeRing Index
derive newtype instance commutativeRingOption :: CommutativeRing Option
derive newtype instance commutativeRingColumn :: CommutativeRing Column
derive newtype instance commutativeRingRow :: CommutativeRing Row
derive newtype instance commutativeRingBox :: CommutativeRing Box
derive newtype instance commutativeRingCell :: CommutativeRing Cell

derive newtype instance euclideanRingIndex :: EuclideanRing Index
derive newtype instance euclideanRingOption :: EuclideanRing Option
derive newtype instance euclideanRingColumn :: EuclideanRing Column
derive newtype instance euclideanRingRow :: EuclideanRing Row
derive newtype instance euclideanRingBox :: EuclideanRing Box
derive newtype instance euclideanRingCell :: EuclideanRing Cell

derive newtype instance showIndex :: Show Index
derive newtype instance showOption :: Show Option
derive newtype instance showColumn :: Show Column
derive newtype instance showRow :: Show Row
derive newtype instance showBox :: Show Box
derive newtype instance showCell :: Show Cell

class IsInt a where toInt :: a -> Int
instance ciInt :: IsInt Int where toInt a = a
instance ciIndex :: IsInt Index where toInt (Index a) = a
instance ciOption :: IsInt Option where toInt (Option a) = a
instance ciColumn :: IsInt Column where toInt (Column a) = a
instance ciRow :: IsInt Row where toInt (Row a) = a
instance ciBox :: IsInt Box where toInt (Box a) = a
instance ciCell :: IsInt Cell where toInt (Cell a) = a

很可能,我很快就会需要

还有其他一些人。

所以现在,这阻止了我添加/减去一个

和一个

意外,或者将参数与操作这些类型的函数混淆。这也是一个相当多的样板文件,以便有效地为Int提供7个名称。

我猜这是大材小用了,也许我不该费心?我可以使用类型同义词来保持代码的描述性,并失去一些类型安全性。

有没有其他/更好的方法?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-01 10:08:14

就像生活中的一切一样,没有什么秘诀。这是一种光谱。在一个极端上根本没有类型安全,在另一个极端上则淹没在新类型中。一如既往,事实总是介于两者之间。

但要告诉你更具体的事情,我需要知道更大的设计。仅仅从这些定义上说是不可能的。可能你真的需要所有的东西,在这种情况下...好吧..。你确实需要他们所有的人。

但根据我的经验,通常情况并非如此。一些需要思考的问题可能是:

它们真的都需要

什么?不知何故,我对此表示怀疑。

它们真的都需要分开吗,还是可以将其中一些合并到一种类型中?例如,

可能是一个

在一起。

如果您希望您的类型具有一个数字的所有特征,那么它可能实际上应该是一个数字?

是否可以通过使用命名参数(通过记录)来实现类型安全?

我通常做这些事情的方式是从基本框架开始,然后在需要的时候添加对东西的支持。您可能会惊讶于在实践中出现的需求是如此之少。

这一点首先适用于使用新类型。你真的有可能把行和列搞混吗?或者你只是想把每一个可能的漏洞都堵上以防万一?如果是后者,那么是的,你确实会得到大量的孔塞,这也就不足为奇了。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66415643

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档