Introductory example in README does not compile
thomashoneyman opened this issue · 0 comments
thomashoneyman commented
A user in #purescript-beginners noticed that the provided quick start example for this library fails to compile as it is treating PureScript records as if they were Haskell records -- at the time of writing, that's
import Options.Applicative
import Data.Semigroup ((<>))
data Sample = Sample
{ hello :: String
, quiet :: Boolean
, enthusiasm :: Int }
sample :: Parser Sample
sample = Sample
<$> strOption
( long "hello"
<> metavar "TARGET"
<> help "Target for the greeting" )
<*> switch
( long "quiet"
<> short 'q'
<> help "Whether to be quiet" )
<*> option int
( long "enthusiasm"
<> help "How enthusiastically to greet"
<> showDefault
<> value 1
<> metavar "INT" )with the following error caught at the first strOption:
Could not match type
String
with type
{ enthusiasm :: Int
, hello :: String
, quiet :: Boolean
}
It can be fixed with this small rewrite:
newtype Sample = Sample
{ hello :: String
, quiet :: Boolean
, enthusiasm :: Int
}
sample :: Parser Sample
sample = map Sample $ { hello: _, quiet: _, enthusiasm: _ }
<$> strOption
( long "hello"
<> metavar "TARGET"
<> help "Target for the greeting" )
<*> switch
( long "quiet"
<> short 'q'
<> help "Whether to be quiet" )
<*> option int
( long "enthusiasm"
<> help "How enthusiastically to greet"
<> showDefault
<> value 1
<> metavar "INT" )though it may be worth just introducing the ado version directly, as in practice I've found that's the easiest way to write and read these:
sample :: Parser Sample
sample = ado
hello <- strOption $ fold
[ long "hello"
, metavar "TARGET"
, help "Target for the greeting"
]
quiet <- switch $ fold
[ long "quiet"
, short 'q'
, help "Whether to be quiet"
]
enthusiasm <- option int $ fold
[ long "enthusiasm"
, help "How enthusiastically to greet"
, showDefault
, value 1
, metavar "INT"
]
in Sample { hello, quiet, enthusiasm }The other examples in the README may also be affected, but I haven't manually checked them.
srghma commented