ScopedTypeVariables不会将类型变量纳入作用域 [英] ScopedTypeVariables doesn't bring type variables into scope
本文介绍了ScopedTypeVariables不会将类型变量纳入作用域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是一个返回指针对齐的简单函数:
{-# LANGUAGE ScopedTypeVariables #-}
import Foreign.Ptr (Ptr)
import Foreign.Storable (Storable, alignment)
main = return ()
ptrAlign1 :: (Storable a) => Ptr a -> Int
ptrAlign1 _ = alignment (undefined :: a)
但我收到以下错误:
Could not deduce (Storable a0) arising from a use of `alignment'
from the context (Storable a)
bound by the type signature for
ptrAlign1 :: Storable a => Ptr a -> Int
at prog.hs:8:14-41
The type variable `a0' is ambiguous
如果我在更杂乱的派系中重写ptrAlign
:
ptrAlign2 :: (Storable a) => Ptr a -> Int
ptrAlign2 = ptrAlign3 undefined where
ptrAlign3 :: (Storable a) => a -> Ptr a -> Int
ptrAlign3 x _ = alignment x
运行正常(当然此版本甚至不需要ScopedTypeVariables
)。
但我仍然很好奇为什么第一个版本会抛出错误,以及可以做些什么来解决它?
推荐答案
即使打开ScopedTypeVariables
,类型变量也不会放入作用域,除非显式量化它们,即
ptrAlign1 :: forall a. (Storable a) => Ptr a -> Int
ptrAlign1 _ = alignment (undefined :: a)
这篇关于ScopedTypeVariables不会将类型变量纳入作用域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文