Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make ReadArgs 1.0 work with a single argument

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:

  1. How does readArgs work?
  2. 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.

like image 790
Dan Burton Avatar asked Jan 06 '12 02:01

Dan Burton


1 Answers

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:

(foo :: Int) :& (bar :: String) :& () <- readArgs

This works trivially with one argument:

(foo :: Int) :& () <- readArgs
like image 197
hammar Avatar answered Sep 23 '22 19:09

hammar