Re: [Haskell-cafe] sizeOf on a type

Hi,
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.
Maybe Foreign.Marshal.Array.advancePtr is what you really need in this case. Gergely -- http://www.fastmail.fm - The professional email service

Yay, advancePtr is exactly what I needed! I totally missed that one in the docs. Also thanks to those of you who pointed me to the scoped type variables feature, since I had figured that a feature liked that had to exist but I just didn't know where to look for it. You guys rock! Cheers, Greg On Dec 18, 2009, at 12:09 PM, Patai Gergely wrote:
Hi,
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.
Maybe Foreign.Marshal.Array.advancePtr is what you really need in this case.
Gergely
-- http://www.fastmail.fm - The professional email service
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Fri, 18 Dec 2009, Gregory Crosswhite wrote:
Yay, advancePtr is exactly what I needed! I totally missed that one in the docs.
Also thanks to those of you who pointed me to the scoped type variables feature, since I had figured that a feature liked that had to exist but I just didn't know where to look for it.
You do not need the Scoped Type Variables feature here, although it may simplify writing. I remember we had a similar thread here, recently. A solution can be: sizeOfPtr :: Ptr a -> a -> Int sizeOfPtr _ x = sizeOf x to be called by (sizeOfPtr ptr undefined). I have added this example to: http://www.haskell.org/haskellwiki/Scoped_type_variables#Avoiding_Scoped_Typ... But in order to be found in Category:FAQ we might need a different title.
participants (3)
-
Gregory Crosswhite
-
Henning Thielemann
-
Patai Gergely