A clearer annotation in the binding chain

So, I have this working example (Snap + Postgres):

listBooks :: AppHandler ()
listBooks = do
  results <- query_ "select * from books"
  writeJSON $ (results :: [Book])

As an exercise, I try to rewrite this with the help of the operator >>=, but the need to annotate the type kills aesthetics. I could not think of anything better:

query_ "select * from books" >>= return.(\x -> x :: [Book]) >>= writeJSON

Is there any other "smoother" way? (preferably without specifying the type of wrapping monad)

+4
source share
2 answers

You can cut it a little to

query_ "select * from books" >>= writeJSON . (\x -> x :: [Book])

Also, not yet, although there are plans to implement a GHC extension for shortcut syntax

(:: [Book]) = (\x -> x :: [Book])

Looking at the @duplode link it looks like the code for it started writing 3 days ago, so it should be in the next GHC release.

+7
source

PartialTypeSignatures, :

query_ "select * from books" >>= (writeJSON :: [Book] -> _)

query_; do :

listBooks :: AppHandler ()
listBooks = do
  results <- query_ "select * from books" :: _ [Book]
  writeJSON results

GHC . , writeJSON query_ , :

query_ @[Book] "select * from books" >>= writeJSON
query_ "select * from books" >>= writeJSON @[Book]

, , ,

query_ "select * from books" >>= asAppliedTo [Book{}] writeJSON

Book - Book ( , [Book{}], , ). , , asAppliedTo - , hoogle ; :

asAppliedTo :: arg -> (arg -> result) -> (arg -> result)
asAppliedTo _ = id
+5

All Articles