I want to print the internal value of a newtype
by deriving Show
in the following way, so that I don't have to unwrap Value val
every time it needs to be printed.
{-# LANGUAGE OverloadedStrings #-}
import Data.Text
newtype Value = Value Text
instance Show Value where show (Value val) = show val
main :: IO ()
main = do
let hello = Value "hello"
world = Value "world"
print $ show hello <> ", " <> show world
pure ()
The idea being that I can simply show hello
instead of doing let Value helloVal = hello in show helloVal
(a bit of a contrived example but the main point is to avoid unwrapping).
The issue is that this prints the following:
"\"hello\", \"world\""
Whereas the desired result would be:
hello, world
How to get to the desired output?
The general convention of Show
is that it should produce more or less Haskell code, which, in most simple cases, you could just copy out of GHCi output and plug right back into its input.
This is why show "foo"
produces a string "\"foo\""
- with quotes. So that when it's printed in GHCi, you see the quotes. This is also why default derived instance for your type produces "Value \"foo\""
- that's straight up Haskell code that can be compiled and evaluated to produce the original Value
value.
But if you really don't want the extra quotes - sure, just convert your Text
to String
using unpack
:
instance Show Value where show (Value v) = unpack v