At 2002-01-25 14:00, Hal Daume III wrote:

>> class D a where constMember :: Int
>> instance D Int where constMember = 8
>
>It seems ehre that there's no way to extract constMember for a
>/particular/ class, since you can't tell it what "a" is supposed to
>be.  So, instead, I do:
>
>> class D a where constMember :: a -> Int -- should be independent of arg
>> instance D Int where constMember _ = 8
>
>(which brings me to my use of "undefined::a").  But is there a
>better/preferred way to do this?

Unfortunately this seems to be the preferred way to do this. For
instance, from the Prelude:

  class  (RealFrac a, Floating a) => RealFloat a  where
    floatRadix       :: a -> Integer
    floatDigits      :: a -> Int

It's not the _best_ way to do it, in my opinion. I do this:

  data Type a = Type

  class D a where
    constMember :: Type a -> Int

  instance D Int where
    constMember Type = 8

  intConst = constMember (Type :: Type Int)

See
<http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/*checkout*/jvm-bridge/sourc
e/Haskell/Type.hs?rev=HEAD&content-type=text/plain>.

Why is this better?

1. It's absolutely clear to the user of the class that constMember
depends only on the type.

2. It prevents the user from putting in an inappropriate "back channel"
in the member for some instance of the class.

3. It avoids use of 'undefined', which is just plain ugly. After all,
intuitively everything is defined.

--
Ashley Yakeley, Seattle WA
Almost empty page: <http://semantic.org/>


_______________________________________________
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell

Reply via email to