faylang / fay

A proper subset of Haskell that compiles to JavaScript
https://github.com/faylang/fay/wiki
BSD 3-Clause "New" or "Revised" License
1.29k stars 86 forks source link

Show instance for Data.Text #453

Closed schnecki closed 5 years ago

schnecki commented 6 years ago

Hi, I figured that there is now Show instance for Data.Text. Is there a reason for this?

Best regards Manuel

bergmark commented 6 years ago

in fay we don’t have typeclasses but we have

show :: Automatic a -> String
show = ffi "JSON.stringify(%1)"

so you should be able to show aText

schnecki commented 6 years ago

Well I have following data structure (which is shared among the haskell server and js client using yesod).

data PatchResult = PatchCacheOk
                 | PatchUpdate String (Maybe Text) (Maybe Text) (Maybe Text)
                 deriving (Typeable, Data)

I would like to be able to build a String out of the type, so I added Show to the deriving instances and end up with:

• No instance for (Show Text)
    arising from the second field of ‘PatchUpdate’ (type ‘Maybe Text’)
  Possible fix:
    use a standalone 'deriving instance' declaration,
      so you can specify the instance context yourself
  There are instances for similar types:
    instance Show text-1.2.2.1:Data.Text.Internal.Text
      -- Defined in ‘text-1.2.2.1:Data.Text.Show’
    instance Show text-1.2.2.1:Data.Text.Internal.Lazy.Text
      -- Defined in ‘text-1.2.2.1:Data.Text.Lazy’
• When deriving the instance for (Show PatchResult)

But I see. I can add the instance only for the server side using preprocessor definitions.

#ifdef FAY
             deriving (Typeable, Data)
#else
             deriving (Show, Typeable, Data)
#endif

Interestingly though, compilation works for data types where I only have a list of Strings:

data ApiResult = ApiResult
             { resultColumnName :: [String]
             , resultValue      :: [String]
             } deriving (Data, Show)

This doesn't seem right to me. Anyways, I know how to work with it. Thx

bergmark commented 6 years ago

I see,

I think it would make sense to add the Show instance to Data.Text in fay-base to make this smoother. fay itself will ignore it. This is what we do for Data & Typeable.