问题描述
我有一个类为类型提供了一个全局唯一标识符:
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'。从
几乎可以工作:
>>>数据D = C
>>>实例名为D
>>> nameOf C
(,Main,D)
但我可以使用 GHC.Generics
获取数据类型的包:
class Named 'f where nameOf':: fa - > (String,String,String)
instance(Datatype t)=>命名'(M1 D tf)其中nameOf'd =(,moduleName d,datatypeName d)
我可以吗?如果没有这个包,GUI不是真正的全局唯一的。
可以这样写: >>> import Data.Typeable
>>> :set -XDeriveDataTypeable
>>>让nameOf =(\ t - >(tyConPackage t,tyConModule t,tyConName t))。 typeRepTyCon。 typeRep
>>>数据D = C导出可键入
>>> nameOf(Proxy :: Proxy D)
(interactiveGhci3D)
我可以这样做。但我很好奇 GHC.Generics
。
到目前为止使用泛型不可能获得软件包名称。现在有一个。实施起来非常简单,但是让我们看看补丁版何时发布版本。
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
.
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.
这篇关于"&的packageName QUOT;与GHC.Generics的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!