I have a class that provides a globally unique identifier for types:
class Named a where
nameOf :: a -> (String,String,String) -- (Package, Module, Identifier)
default nameOf :: (Generic a, Named' (Rep a)) => a -> (String,String,String)
nameOf = nameOf' . from
which almost works:
>>> data D = C
>>> instance Named D
>>> nameOf C
("","Main","D")
but I can't get the datatype's package with GHC.Generics
:
class Named' f where nameOf' :: f a -> (String,String,String)
instance (Datatype t) => Named' (M1 D t f) where nameOf' d = ("", moduleName d, datatypeName d)
Can I? The GUI's not really "globally" unique without the package.
btw, I know that with Data.Typeable
I can write:
>>> import Data.Typeable
>>> :set -XDeriveDataTypeable
>>> let nameOf = (\t -> (tyConPackage t, tyConModule t, tyConName t)) . typeRepTyCon . typeRep
>>> data D = C deriving Typeable
>>> nameOf (Proxy :: Proxy D)
("interactive" "Ghci3" "D")
Which is what I may do. But I'm curious about GHC.Generics
.
PackageImports
? (haven't used it, just heard about it) – FatmaPackageImports
you can import the same module from different packages, even into the same module: gist.github.com/phadej/8b628d579ddf6958d937 Didn't ever tried to do anything like that! – Irizarry