Re: Combining RequiredTypeArguments and type class methods?
Tom Ellis <[email protected]> Sun, 13 Jul 2025 11:47:36 +0100
| Newsgroups | gmane.comp.lang.haskell.cafe |
|---|---|
| Message-ID | <aHOOyJHSTtC/HRdO@cloudinit-builder> |
On Sun, Jul 13, 2025 at 04:03:08PM +1000, Viktor Dukhovni wrote:
> The user guide documentation of RequiredTypeArguments highlights a
> hypothetical alternative to the interface of the `sizeOf` method of
> the Storable class:
>
> sizeOf :: forall a -> Storable a => Int
>
> This is fine in isolation, but how would this actually work in a type
> class method definition?
>
> Is the hypothetical `sizeOf` actually realisable as a type class method?
> Or can it only be a module-level wrapper? Something like the below,
> which does work?
Yeah, there's definitely something that doesn't quite work with
RequiredTypeArguments and type class methods. After all, the type of a
type class `C` is implicitly prefixed with `forall a. C a =>`, so you
can't add your own `forall a ->`. It would shadow.
There is, however, a known trick. This works:
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE RequiredTypeArguments #-}
class MyStorable a where
sizeOf :: forall b -> b ~ a => (MyStorable a) => Int
instance MyStorable Bool where
sizeOf (type Bool) = 1
(But I prefer your solution: the wrapper.)
Tom
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.