यहाँ एक सूचक के संरेखण वापस जाने के लिए एक सरल समारोह है:ScopedTypeVariables दायरे में प्रकार चर नहीं लाती
{-# LANGUAGE ScopedTypeVariables #-}
import Foreign.Ptr (Ptr)
import Foreign.Storable (Storable, alignment)
main = return()
ptrAlign1 :: (Storable a) => Ptr a -> Int
ptrAlign1 _ = alignment (undefined :: a)
लेकिन मैं निम्नलिखित त्रुटि मिलती है: यदि मैं में ptrAlign
पुनर्लेखन
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
इस तरह एक और गन्दा गुट:
ptrAlign2 :: (Storable a) => Ptr a -> Int
ptrAlign2 = ptrAlign3 undefined where
ptrAlign3 :: (Storable a) => a -> Ptr a -> Int
ptrAlign3 x _ = alignment x
यह ठीक काम करता है (बेशक यह संस्करण ScopedTypeVariables
की भी आवश्यकता नहीं है)।
लेकिन मैं अभी भी उत्सुक हूं कि पहला संस्करण एक त्रुटि क्यों फेंक रहा है, और इसे हल करने के लिए क्या किया जा सकता है? ScopedTypeVariables
इस के पीछे तर्क क्या है? –
@ErikAllik: [मैनुअल] (https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/other-type-extensions.html#scoped-type-variables) नहीं कहता है, लेकिन मुझे उम्मीद है कि ऐसा है कि मौजूदा कोड में 'स्कोप्ड टाइप टाइपरीबल्स' को चालू करके इसका व्यवहार नहीं बदला गया है। –