在纯脚本中获取新类型的记录基础类型

Get a newtype'd records underlying type in purescript

我正在尝试查看是否有一种简单的方法来获取新类型记录的类型以放入函数签名中。

newtype T1 = T1 { foo:: Int}
derive instance newtypeT1 :: Newtype T1 _ 
... other classes that require me to newtype the record ...

我知道我可以访问记录成员 _.property 我可以用 unwrap 组合它 unwrap >>> _.property 来获取 属性 的函数,但我想编写一个类似于

的函数
testFoo :: forall a. (_ -> a) -> Effect a
testFoo accessor = (unwrap >>> accessor) <$> loadT1

这有效,但通配符给出警告,但我不确定如何从 T1 获取该记录定义。 (这是一个最小的例子,我有一个来自外部源的巨大 属性 对象。

我现在一直在使用的解决方法是像这样声明我的类型

 type InnerT1 = { foo ::Int}
 newtype T1 = T1 InnerT1

并导出 InnerT1 以便在我的测试文件中使用,但这似乎有点笨拙,我想知道是否有更好的方法?

您可以使用 Newtype class 获取内部类型:

testFoo :: forall a inner. Newtype T1 inner => (inner -> a) -> Effect a 
testFoo accessor = (unwrap >>> accessor) <$> loadT1

这不需要额外的注释就可以工作,因为 class 具有函数依赖性 Newtype a b | a -> b,这意味着内部类型由外部类型唯一确定。