如果我想声明一个
```haskell newtype MyType a = MyType {unMyType :: a} deriving (SomeTypeclass) ```
```haskell newtype MyType a = MyType {unMyType :: a} deriving (SomeTypeclass) ```
newtype
,以便该值的类型受限于必须具有某个类型类的实例,似乎可以使用以下方法:```haskell newtype MyType a = MyType {unMyType :: a} deriving (SomeTypeclass) ```
```haskell newtype MyType a = MyType {unMyType :: a} deriving (SomeTypeclass) ```
{-# LANGUAGE RankNTypes #-}
newtype ShowBox = ShowBox (forall a. Show a => a)
GHC可以很好地编译它,但是当我尝试使用ShowBox
时,出现了问题。
ShowBox "hello"
我遇到了编译器错误。
<interactive>:1:18:
Could not deduce (a ~ [Char])
from the context (Show a)
bound by a type expected by the context: Show a => a
at <interactive>:1:10-24
`a' is a rigid type variable bound by
a type expected by the context: Show a => a at <interactive>:1:10
In the first argument of `ShowBox', namely `"hello"'
In the expression: ShowBox "hello"
In an equation for `a': a = ShowBox "hello"
有没有办法让这个工作?
ShowBox
只能应用于精确类型为Show a => a
的值。我非常想知道这个问题的答案。 - Daniel Pratt