I have a basic question about definining instances of typeclasses. I am using the Show typeclass as an example, and I am considering only the function show within the class. The Show instance for a concrete type like Bool is simple
instance Show Bool where
show x = {function of x here}
but for String it is not:
instance Show String where
show x = {function of x here}
produces understandably an error
Illegal instance declaration for ‘Formatter String’
(All instance types must be of the form (T t1 ... tn)
where T is not a synonym.
Use TypeSynonymInstances if you want to disable this.)
In the instance declaration for ‘Formatter String’
and of course the following is not allowed:
instance Show [Char] where
show x = {function of x here}
I could define a newtype
newtype String2 = String2 String
instance Formatter String2 where
format (String2 x) = {function of x here}
which however does not allow me to do show "test", as I am able to do in Haskell.
What essential feature of typeclasses am I missing?