使 ReadArgs 1.0 使用单个参数
使用 ReadArgs 包,它似乎不支持单参数情况。
{-# LANGUAGE ScopedTypeVariables #-}
import ReadArgs (readArgs)
main = do
(foo :: Int) <- readArgs
print foo
错误是(当使用版本 1.0 时):
No instance for (ReadArgs.ArgumentTuple Int)
arising from a use of `readArgs'
我的问题有两个:
readArgs
是如何工作的?- 如何调整该库以使其也可以使用单个参数?
NB ReadArgs 1.1 版本消除了这个“错误”;见评论。
Playing around with the ReadArgs package, it seems that it does not support single-argument situations.
{-# LANGUAGE ScopedTypeVariables #-}
import ReadArgs (readArgs)
main = do
(foo :: Int) <- readArgs
print foo
The error is (when using version 1.0):
No instance for (ReadArgs.ArgumentTuple Int)
arising from a use of `readArgs'
My question is twofold:
- How does
readArgs
work? - How can that library be adjusted to allow it to work with a single argument as well?
N.B. version 1.1 of ReadArgs eliminates this "error"; see comments.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
据我所知,该包使用元组来模拟类型安全的异构列表。正如您所注意到的,当您只需要一个参数时,这会导致问题,因为 Haskell 中没有一元组。
但是,该包还为异构列表提供了适当的类型,可以使用它来代替元组:
:&
类型。您可以使用类似于:
运算符的方式使用它,并使用空元组()
作为终止符:这对于一个参数来说很简单:
From what I can tell, the package uses tuples to emulate type-safe heterogeneous lists. As you noticed, this causes problems when you want only one argument, as there are no one-tuples in Haskell.
However, the package also provides a proper type for heterogeneous lists, which can be used instead of tuples: the
:&
type. You use it similar to the:
operator, with the empty tuple()
as a terminator:This works trivially with one argument:
另一个答案,只是为了好玩:具体化 Daniel Wagner 为
OneTuple
添加实例的建议:大部分是从 Adam Wagner 的解决方案中窃取的。令人惊讶的是,所有额外的语言编译指示都可以被删除。
这可能听起来很愚蠢,但由于某种原因,OneTuple (foo :: Int) <- readArgs 并不像我想象的那么难看。
Another answer, just for fun: reifying Daniel Wagner's suggestion of adding an instance for
OneTuple
:Mostly stolen from Adam Wagner's solution. Amazingly, all the extra language pragmas can be removed.
This may sound silly, but the
OneTuple (foo :: Int) <- readArgs
isn't nearly as ugly as I imagined it would be for some reason.我不太明白我需要启用的所有扩展,但是您可以定义一个
ReadArgs.ArgumentTuple a
的实例(即使它实际上不是一个语义上正确的名称),如下所示:另外,我我不太确定这个实例是否有任何问题,尽管它适用于您提供的示例。我认为图书馆丢失它是有原因的,但我可能是错的。
更新
在尝试了一些事情之后,为了确保它们仍然有效(如下面的示例),我相当确信这不会导致任何问题,所以也许它(或类似的东西)的存在只是一个疏忽。
I don't quite understand all of the extensions I needed to enable, but you could define an instance of
ReadArgs.ArgumentTuple a
(even though it's not really a semantically correct name) like this:Also, I'm not really sure if there are any problems with this instance, though it works for the example you presented. I would assume there's a reason it's missing from the library, but I may be wrong.
Update
After looking trying a few things out, to be sure they still work (like the following example), I'm fairly convinced this doesn't cause any problems, so maybe it's (or something similar) existance was just an oversight.
一种迟钝的解决方案是滥用“可选参数”功能:
即使您提供
()
作为第二个参数,它也会默默地工作:并稍微搞乱使用消息:
但它确实拒绝额外的参数不是
()
,并且它确实按预期工作:One retarded solution is to abuse the "optional argument" feature:
This silently works even if you supply
()
as the second arg:And screws up the usage message a little:
But it does reject extra arguments that are not
()
, and it does work as desired: