ITADN

A functional version of withShownDefault

#16Closedpbrisbin 创建于 2024-11-01
P
pbrisbincommented
I have a custom type for bytes that I use as an option with a better syntax: something like `"128m"` -> `Bytes 128 $ Just M`. `withShownDefault` is perfect for getting `default: 128m` to appear in the documentation, but its ergonomics were not what I expected. Ultimately, I had to write: ```hs withShownDefault (Bytes 128 $ Just M) (showBytes $ Bytes 128 $ Just M) $ setting [ help "Run restylers with --memory" , option , name "memory" , metavar "NUMBER<b|k|m|g>" , reader $ eitherReader readBytes ] ``` I have to specify the default value twice, which could lead to bugs. I know I could extract with `let` or `where`, of course, but that's not always convenient. When I first saw `withShownDefault` I assumed it would work like this: ```hs withShownDefault :: (a -> String) -> a -> Parser a -> Parser a withShownDefault = undefined withDefault :: a -> Parser a -> Parser a withDefault = withShownDefault show ``` Then I could simply do, ```hs withShownDefault showBytes (Bytes 128 $ Just M) $ setting [ help "Run restylers with --memory" , option , name "memory" , metavar "NUMBER<b|k|m|g>" , reader $ eitherReader readBytes ] ``` I'm interested in adding a function that works this way, but with the name `withShownDefault` taken, I'm not sure what to call it. ```hs withShownByDefault :: (a -> String) -> a -> Parser a -> Parser a withShownByDefault f a = withShownDefault (f a) a ``` ?
关闭于 2024-11-05 5 条评论