Support for Rational configuration values #3

Closed
basvandijk opened this Issue Oct 3, 2011 · 2 comments

Projects

None yet

2 participants

@basvandijk
Collaborator

Hi Bryan,

I would like to use configurator but I need to be a able to specify Rational configuration values.

I see two ways of adding these:

  • Change the Number constructor from: Number Integer to: Number Number where the Number type will be defined in attoparsec-text (or just in configurator) and will be similar to the Number type from attoparsec but uses a Rational instead of a Double. See the following for an implementation. The following number parser should also be defined:
number :: Parser Number
number = floaty $ \real frac fracDenom ->
         if frac == 0 && fracDenom == 0
         then I real
         else R $! asRational real frac fracDenom

asRational :: Integer -> Integer -> Integer -> Rational
asRational real frac fracDenom = real % 1 + frac % fracDenom
  • Change the Number constructor from: Number Integer to: Number Rational. In the previous solution, if a user wants to check if a number is an integer or a rational she can write a pattern match. In this solution this is still also possible by checking if the denominator is 1.

What do you think?

(In my own private code I will go for the latter solution as that is much simpler than the former.)

@basvandijk basvandijk added a commit to basvandijk/configurator that referenced this issue Oct 3, 2011
@basvandijk basvandijk Changed Number constructor from Integer to Rational
Fixes issue #3
acf1f73
@bos
Owner
bos commented Nov 17, 2011

Your changes don't compile:

Data/Configurator/Parser.hs:87:22:
No instance for (RealFloat Rational)
  arising from a use of `rational'
Possible fix: add an instance declaration for (RealFloat Rational)
In the second argument of `(<$>)', namely `rational'
In the expression: Number <$> rational
In the first argument of `mconcat', namely
  `[string "on" *> pure (Bool True),
    string "off" *> pure (Bool False),
    string "true" *> pure (Bool True),
    string "false" *> pure (Bool False), ....]'
@basvandijk
Collaborator

Thanks for merging this Bryan!

@basvandijk basvandijk closed this Nov 21, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment