3

I have a splice I'm writing in TH which needs to take a list of module names and create new names based on a convention I have for these modules. At the moment I am doing this by passing in Strings of the module names, but I'm not thrilled by this and it seems like there should be a name to refer to the module more ... programmatically.

This is the splice:

link ::
  String ->
  [String] ->
  Name ->
  Q [Dec]
link prefix features monadName = do
  let routeName = mkName $ capitalise prefix <> "Routes"
      handlerName = mkName $ lowerFirst prefix <> "Handler"
  routes <- mkRoutes routeName ((<> ".Routes") <$> features)
  handler <- mkHandler handlerName routeName monadName ((<> ".routeHandler") <$> features)
  pure $ routes : handler

So, as you can see, it basically adds .Routes and .routeHandler onto the module names, and then passes those into other functions to construct the actual declarations (I'm generating Servant ServerT declarations).

Is there a way to refer to the module itself and then to construct new names from it? Something like:

import Project.Feature.MyCoolFeature qualified as MyCoolFeature

-- ...
-- this doesn't work, but it's the sort of thing I want:
moduleName :: Name
moduleName = ''MyCoolFeature
memberName :: Name
memberName = moduleName <.> mkName "Routes"
GTF
  • 8,031
  • 5
  • 36
  • 59

0 Answers0