Если я хочу объявить newtype
таким образом, чтобы тип значения был ограничен наличием экземпляра для класса типов, кажется, что я могу это сделать with:
{-# 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"
Есть ли способ заставить это работать?