So, I could accomplish this by using 'topLevelSomething
and removing the last token after ., or I could use moduleName 'something
but that returns a Maybe...
Is there a more straightforward way to get the module name of the current context?
So, given the code:
module My.Module.Blah where
test = magicHere
What goes in that magicHere spot such that test = "My.Module.Blah" ?
Haskell's module design is relatively conservative: the name-space of modules is completely flat, and modules are in no way "first-class." Module names are alphanumeric and must begin with an uppercase letter. There is no formal connection between a Haskell module and the file system that would (typically) support it.
A Haskell module is a collection of related functions, types and typeclasses. A Haskell program is a collection of modules where the main module loads up the other modules and then uses the functions defined in them to do something. Having code split up into several modules has quite a lot of advantages.
I thought this was a nice question, so I figured out the answer using Template Haskell:
{-# LANGUAGE TemplateHaskell #-}
module A.B.C where
import Language.Haskell.TH
import Language.Haskell.TH.Syntax
e :: String
e = $(fmap loc_module qLocation >>= \mod -> return (LitE (StringL mod) ))
main = print e
There's a rather roundabout way to get the current module name using Typeable.
module My.Module.Blah where
import Data.Typeable
data T = T deriving Typeable
test = init $ init $ show $ typeOf T
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With