简体   繁体   English

在我自己的队列类型中包装Data.Sequence有什么问题吗?

[英]Is there anything wrong with wrapping Data.Sequence in my own Queue type?

I want a queue for my application. 我想为我的应用程序排队。 From what I've read, Data.Sequence is the best option for production code. 根据我的阅读, Data.Sequence是生产代码的最佳选择。 In order to keep my code elegant, idiomatic, and overall Haskelly, is there anything wrong with wrapping Data.Sequence up in my own Queue data type in order to hide the functionality I don't need? 为了保持我的代码优雅,惯用和整体Haskelly,在我自己的Queue数据类型中包装Data.Sequence以隐藏我不需要的功能有什么问题吗?

Nothing wrong at all - in fact, that's precisely what newtype is for! 没有错的话-事实上,这正是newtype是!

Ideally, your types should reflect your intentions - whoever reads your code (including yourself, in the future, when you have forgotten what you did and why) should be able to tell what a type is for 理想情况下,你的类型应该反映你的意图-谁读你的代码(包括你自己,在未来,当你忘记了你做了什么以及为什么)应该能够告诉类型是什么

So, even if your queue is merely a Sequence , wrapping it into your own Queue newtype will tell the (human) readers "this particular sequence is to be used for queuing" - besides the benefits of hiding non-required functionality, and preventing mixing up your values (ie no accidentally passing another Sequence that is not supposed to be a queue to a function that expects a queue). 因此,即使您的队列仅仅是一个Sequence ,将其包装到您自己的Queue也会告诉(人类)读者“这个特定序列将被用于排队” - 除了隐藏非必需功能和防止混合的好处你的值(即不会意外地传递另一个应该是队列的Sequence到一个需要队列的函数)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM