2

I'm working through the reflex-frp examples on the github page and got stuck on the Dynamics and Events section. I try to use ghcjs to compile the following code:

{-# LANGUAGE OverloadedStrings #-}

import Reflex
import Reflex.Dom

main = mainWidget $ el "div" $ do
    t <- textInput def
    text "Last key pressed: "
    let keypressEvent = fmap show $ _textInput_keypress t
    keypressDyn <- holdDyn "None" keypressEvent
    dynText keypressDyn

but I get the error

eventTest.hs:11:13: error:
    • Couldn't match type ‘[Char]’ with ‘Data.Text.Internal.Text’
      Expected type: Dynamic
                       (SpiderTimeline Global) Data.Text.Internal.Text
        Actual type: Dynamic (SpiderTimeline Global) String

I understand that the String should be parsed instead as Data.Text.Internal.Text, but don't know how to make this happen - I thought the OverloadedStrings statement at the top was supposed to resolve this issue. Does anybody know how to fix this error?

Chandler Squires
  • 397
  • 3
  • 7
  • 22
  • 2
    `OverloadedString` ensures that a string literal has type `IsString a => a` instead of simply `String`. It doesn't directly affect the type signatures of functions that are already defined to work with `String` values. – chepner Sep 21 '16 at 18:13
  • 5
    I suspect the problem is that `show` returns a `String`; does `fmap (pack . show) $ _textInput_keypress t` work? – chepner Sep 21 '16 at 18:26
  • For Haskell experts but reflex non-experts: [`holdDyn :: MonadHold t m => a -> Event t a -> m (Dynamic t a)`](http://hackage.haskell.org/package/reflex-0.4.0/docs/Reflex-Dynamic.html#v:holdDyn). – Daniel Wagner Sep 21 '16 at 20:10
  • 2
    Changing `show` to `tshow`, where `tshow x = pack $ show x`, as per chepner's suggestion, worked for me – Chandler Squires Sep 21 '16 at 23:22
  • 1
    @ChandlerSquires For engineering reasons I wouldn't use `pack . show` - you're paying the efficiency costs of using `String` _and_ the complexity costs of using `Text`. As a rule `pack` should be avoided. Serialise your data to `Text` directly, using eg [`text-show`](https://hackage.haskell.org/package/text-show) – Benjamin Hodgson Sep 22 '16 at 00:29

0 Answers0