“packageName” with GHC.Generics

不想你离开。 提交于 2020-01-04 04:02:11

问题


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.


回答1:


So far it's impossible to get package name using Generics. There is a GHC feature request -ticket now. It was straightforward to implement, but let's see when the patch lands release version.



来源:https://stackoverflow.com/questions/28159068/packagename-with-ghc-generics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!