我正在公开一个带有两个参数的函数,一个是最小边界,另一个是最大边界。我如何确保使用类型,例如最小边界不大于最大边界?
我想避免创建一个智能构造函数并返回Maybe,因为它会使整个使用更加繁琐。
谢谢 解决方案
这并不能完全回答你的问题,但有时候有效的方法之一是改变你的类型的解释。例如,而不是 data Range = {lo :: Integer,hi :: Integer}
您可以使用
数据范围= {lo :: Integer,size :: Natural}这样,就无法表示无效的范围。
I am exposing a function which takes two parameters, one is a minimum bound and the other is a maximum bound. How can I ensure, using types, that for example the minimum bound is not greater than the maximum bound?
I want to avoid creating a smart constructor and returning a Maybe because it would make the whole usage more cumbersome.
Thank you
解决方案This doesn't exactly answer your question, but one approach that sometimes works is to change your interpretation of your type. For example, instead of
data Range = {lo :: Integer, hi :: Integer}you could use
data Range = {lo :: Integer, size :: Natural}This way, there's no way to represent an invalid range.
更多推荐
将数字限制在一个范围内(Haskell)
发布评论