Sun, 7 May 2000 00:56:57 +1000, Fergus Henderson <[EMAIL PROTECTED]> pisze:
> Incidentally, this is an area where Mercury is more expressive than
> Haskell. In Mercury, dummy arguments are still needed sometimes.
> But using Mercury's mode system, you can express in the function's
> declaration the fact that it depends only on the argument's type,
> not on its value, by using the mode `unused' rather than `in':
>
>:- typeclass foo(A) where [
> func cardinality(A::unused) = (int::out)
> ].
>
> The compiler's static checking will enforce this mode declaration;
> it would be a compile-time error to define an instance of this
> typeclass for which the cardinality function examined the value of
> its argument.
Something like this can be done in Haskell too, although usage of
such function will be a bit more elaborated.
data Tag a = Tag
tag:: a -> Tag a -- Helper function, not strictly necessary.
tag _ = Tag
class Storable a where
sizeOf :: Tag a -> Int
poke :: Ptr a -> a -> IO ()
data Ptr a
mallocBytes:: Int -> Ptr a
mallocAndPoke:: Storable a => a -> Ptr a
mallocAndPoke x = do
ptr <- mallocBytes (sizeOf (tag x))
poke ptr x
return ptr
-- A variant using an extension of pattern type signatures,
-- without the "tag" function:
mallocAndPoke:: Storable a => a -> Ptr a
mallocAndPoke (x::a) = do
ptr <- mallocBytes (sizeOf (Tag::Tag a))
poke ptr x
return ptr
IMHO it is more elegant than sample arguments, clearly states the
intent, and can be easier optimized by a compiler, but it's a bit
less convenient to use, so I don't advocate this.
--
__("< Marcin Kowalczyk * [EMAIL PROTECTED] http://qrczak.ids.net.pl/
\__/ GCS/M d- s+:-- a23 C+++$ UL++>++++$ P+++ L++>++++$ E-
^^ W++ N+++ o? K? w(---) O? M- V? PS-- PE++ Y? PGP+ t
QRCZAK 5? X- R tv-- b+>++ DI D- G+ e>++++ h! r--%>++ y-