[Haskell] Re: Parameterized Show

Keean Schupke k.schupke at imperial.ac.uk
Mon Nov 15 07:38:14 EST 2004


Of course if you want to do it to code independantly of type you need to
redifine show:

    data ShowHex = ShowHex
    class ShowDict t a where
       showDict :: a -> ShowS

    instance ShowDict ShowHex Int where
       showDict a = showHex a

    test :: ShowDict t a => t -> a -> ShowS
    test _ a = showDict a

    main = putStrLn $ (test ShowHex 27) ""

Keean.

Keean Schupke wrote:

> Easy:
>
>    data ShowHex a
>    instance Show (ShowHex a) where
>       showsPrec _ (ShowHex a) = showHex a
>
>    main = putStrLn $ (show (ShowHex 27))
>
> Here, with labelled instances you would write:
>
>    show ShowHex 27
>
> instead you write:
>
>    show (ShowHex 27)
>
>
>    Keean.
>
> George Russell wrote:
>
>> Keean Schupke wrote:
>>
>>> Do you need a language extension at all? You can certainly
>>> do it with the existing extensions!
>>>
>>> data ShowDict a
>>> instance Show (ShowDict a) where
>>>    showsPrec _ (ShowDict a) = ...
>>
>>
>>
>> I don't understand.  How does that help you to, for example, use a 
>> function which
>> requires Show Int but (say) substitute the standard function for 
>> which which shows
>> in hexadecimal?
>>
>> _______________________________________________
>> Haskell mailing list
>> Haskell at haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell
>
>
>
> _______________________________________________
> Haskell mailing list
> Haskell at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell




More information about the Haskell mailing list