
Hello list, Suppose I want show Nothing to return "", and show (Just foo) return show foo. I don't seem to be able to. Looks like I either have to use some other function name, like `mShow', or have to import Prelude hiding Maybe, reimplement Maybe, write all the other useful instances (Functor, Monad) for it, etc. Not particularly hard, but looks ugly. Isn't there a better solution? I recall some discussion about this, but can't find it in the archives... TIA, -- DoubleF No virus detected in this message. Ehrm, wait a minute... /kernel: pid 56921 (antivirus), uid 32000: exited on signal 9 Oh yes, no virus:)

On Feb 2, 2007, at 21:10 , Sergey Zaharchenko wrote:
Hello list,
Suppose I want show Nothing to return "", and show (Just foo) return show foo. I don't seem to be able to. Looks like I either have to use some other function name, like `mShow', or have to import Prelude hiding Maybe, reimplement Maybe, write all the other useful instances (Functor, Monad) for it, etc. Not particularly hard, but looks ugly. Isn't there a better solution? I recall some discussion about this, but can't find it in the archives...
With GHC you can at least avoid rewriting all the instances: make a newtype, use newtype deriving to get all the instances except Show, and write your own Show instance.
{-# OPTIONS_GHC -fglasgow-exts #-}
newtype MyMaybe a = MyMaybe (Maybe a) deriving (Eq,Ord,Monad,Functor)
instance Show a => Show (MyMaybe a) where showsPrec _ (MyMaybe Nothing) = id showsPrec n (MyMaybe (Just x)) = showsPrec n x
/Björn

Hi Sergey, You wrote:
Suppose I want show Nothing to return "", and show (Just foo) return show foo. I don't seem to be able to. Looks like I either have to use some other function name, like `mShow'
That is correct. Show instances are supposed to follow the convention that "show x" is a Haskell expression that recreates x. In other words, Show is mainly used for debugging, for simple serialization, and for interactive use in a Haskell shell like ghci or hugs. If you need to create strings from a datatype for some other reason, use a different function name. If you need to do it for several datatypes, create your own class. Of course, Show does turn out to be more generally useful for numeric types. Hope this helps. Regards, Yitz

Hello Yitzchak! Sat, Feb 03, 2007 at 07:54:17PM +0200 you wrote:
Show instances are supposed to follow the convention that "show x" is a Haskell expression that recreates x. In other words, Show is mainly used for debugging, for simple serialization, and for interactive use in a Haskell shell like ghci or hugs.
If you need to create strings from a datatype for some other reason, use a different function name. If you need to do it for several datatypes, create your own class.
Yes, I think another Show-like class will probably be a better solution... Thanks Yitzchak and Bjorn, -- DoubleF No virus detected in this message. Ehrm, wait a minute... /kernel: pid 56921 (antivirus), uid 32000: exited on signal 9 Oh yes, no virus:)

G'day all.
Quoting Sergey Zaharchenko
Yes, I think another Show-like class will probably be a better solution...
This is the one that I use. Very simple. import Text.PrettyPrint.HughesPJ class Pretty a where -- Equivalent of showsPrec prettyP :: Int -> a -> Doc prettyP _ x = pretty x -- Equivalent of shows pretty :: a -> Doc pretty x = prettyP 0 x Cheers, Andrew Bromage
participants (4)
-
ajb@spamcop.net
-
Bjorn Bringert
-
Sergey Zaharchenko
-
Yitzchak Gale