Is there a way to define multiple type synonyms in a single line?
No. A type definition is its own statement. If you really wanted to, you could group them on the same line by separating with semicolons instead of newlines, but this would be a very very unusual style.
Well, you can of course have any code generated for you by Template Haskell.
module MultiDefinitions where
import Language.Haskell.TH
import Control.Monad
multipleTypeSynonyms :: [Q Name] -> Q Type -> DecsQ
multipleTypeSynonyms synGs tG = do
t <- tG
forM synGs $ \synG -> do
syn <- synG
return $ TySynD syn [] t
Then
{-# LANGUAGE TemplateHaskell #-}
import Language.Haskell.TH
import MultiDefinitions
multipleTypeSynonyms
(newName <$> ["Speed", "Height", "FuelMass", "Gravity"]) [t| Float |]
will generate the same definitions you wrote manually.
Is it a good idea to du stuff like that with TH? Hardly. In fact it is also not clear that type synonyms like that are a good thing at all – many Haskellers would argue that you should either use newtype
s that actually give additional type safety (or use existing library types, such as from the units
library), or else simply hard-code Float
.
BTW there's seldom a good reason to use Float
, just use Double
.