>我正在尝试创建一个包装 [] 的类型实例;即
instance Foo (NonNull []) where
(其中NonNull是Michael Snoyman的数据.NonNull( 这适用于,比如说,
instance Foo [] where
但是有了NonNull []
,我得到了
Expected kind ‘* -> *’, but ‘NonNull []’ has kind ‘*’
我尝试使用RankNTypes来提供明确的forall:
instance ∀ α . Foo (NonNull [α]) where
结果类似:
Expected kind ‘* -> *’, but ‘NonNull [α]’ has kind ‘*’
还有一个类型同义词:
type NonNullList α = NonNull [α]
instance ToSeq NonNullList where
这给了:
The type synonym ‘NonNullList’ should have 1 argument, but has been given none
我相信这一定是可能的,但我错过了咒语。 任何指针都感激地收到。
您的最后一次尝试是最接近的一次,但您需要一个newtype
而不是type
同义词(data
也可以(:
newtype NonNullList α = NonNullList (NonNull [α])
instance ToSeq NonNullList where ...
您首先尝试NonNull []
没有意义,因为NonNull
的参数必须是类型(如[Int]
(,而不是像[]
那样的类型构造函数。您应该会看到第二个错误,如以下类似示例所示:
instance Functor (Maybe []) where
main.hs:5:19: error:
• Expected kind ‘* -> *’, but ‘Maybe []’ has kind ‘*’
• In the first argument of ‘Functor’, namely ‘(Maybe [])’
In the instance declaration for ‘Functor (Maybe [])’
|
5 | instance Functor (Maybe []) where
| ^^^^^^^^
main.hs:5:25: error:
• Expecting one more argument to ‘[]’
Expected a type, but ‘[]’ has kind ‘* -> *’
• In the first argument of ‘Maybe’, namely ‘[]’
In the first argument of ‘Functor’, namely ‘(Maybe [])’
In the instance declaration for ‘Functor (Maybe [])’
|
5 | instance Functor (Maybe []) where
| ^^