
Hey everyone, I would like to write a routine like nextPtr :: Storable a => Ptr a -> Ptr a nextPtr = (`plusPtr` sizeOf (undefined :: a)) which increments a pointer by the correct amount, but GHC complains that the type variable "a" is ambiguous. I can see what's going on: it can't tell that the "a" I am writing there is the same "a" that's in the type specification, but is there any way that I can make it identify "a" with the "a" in the specification for nextPtr? Cheers, Greg

I can see what's going on: it can't tell that the "a" I am writing there is the same "a" that's in the type specification, but is there any way that I can make it identify "a" with the "a" in the specification for nextPtr?
Lexically scoped type variables: http://www.haskell.org/ghc/docs/latest/html/users_guide/other-type-extension... Regards, Sean

Sean Leather wrote:
I can see what's going on: it can't tell that the "a" I am writing there is the same "a" that's in the type specification, but is there any way that I can make it identify "a" with the "a" in the specification for nextPtr?
Lexically scoped type variables: http://www.haskell.org/ghc/docs/latest/html/users_guide/other-type-extension...
And if you require H98+FFI only, you can also use the following: ptrSizeOf = sizeOf . asTypeOf undefined . unsafePerformIO . peek The asTypeOf function is the H98 way of handling many scoped type variable issues. It never evaluates its second argument so the unsafePerformIO is perfectly safe since the peek will never be evaluated either (and, heck, the whole argument to sizeOf won't be evaluated either). -- Live well, ~wren
participants (4)
-
Gregory Crosswhite
-
Sean Leather
-
Thomas DuBuisson
-
wren ng thornton