0

I am a newcomer to Haskell and I am trying to build a simple website with a library called Reflex.Dom and the Cascading Style Sheets (CSS) are represented as a Map Text Text object, that's nice but Haskell is distinguishing between three, nearly-identical objects:

  • String
  • Text
  • [Char]

Certainly, in Python or JavaScript these are the same or nearly the same and functions can be used interchangeably between one object and the other. Not the case in Haskell.

 "style" =: pack("color:red")

how about

 "style" =: pack("color:red; font-family: Helvetica;")

If I add another style element, I get an error message:

square-01.hs:6:47:
    Couldn't match type ‘Text’ with ‘[Char]’
    Expected type: String
      Actual type: Text
    In the second argument of ‘(=:)’, namely ‘pack ("color:red")’
    In the second argument of ‘elAttr’, namely
      ‘("style" =: pack ("color:red"))’

Here is a little bit from Prelude but I don't really get it.

Prelude Reflex.Dom Data.Text Data.Map> "a" =: pack("b")
fromList [("a","b")]

Prelude Reflex.Dom Data.Text Data.Map> :t "a" =: pack("b")
"a" =: pack("b") :: Map [Char] Text

Prelude Reflex.Dom Data.Text Data.Map> "a" =: "b"
fromList [("a","b")]

Prelude Reflex.Dom Data.Text Data.Map> :t "a" =: "b"
"a" =: "b" :: Map [Char] [Char]

Prelude Reflex.Dom Data.Text Data.Map> :t (=:)
(=:) :: k -> a -> Map k a
john mangual
  • 7,718
  • 13
  • 56
  • 95
  • 1
    Add `{-# LANGUAGE OverloadedStrings #-}` to the top of your file and watch these errors vanish. – Alec Feb 21 '17 at 23:33
  • 1
    More generally, `[Char]` and `String` are identical, but `Text` is different. – Alec Feb 21 '17 at 23:33
  • @Alec it's already there – john mangual Feb 21 '17 at 23:35
  • Apologies. My first comment is misplaced. My second comment still holds. To construct a `Map Text Text`, you'll need to add a type annotation. `"a" =: "b" :: Map Text Text`. Basically, with `OverloadedStrings` enabled, all strings have the type `IsString a => a`. Unless otherwise constrained, GHC has some defaulting magic to pick `a ~ String`. – Alec Feb 21 '17 at 23:43
  • 2
    But apparently `elAttr` wants a `Map String String`, so you just need to get rid of the `pack`. (`pack` converts `String` to `Text`.) – Reid Barton Feb 21 '17 at 23:44
  • @ReidBarton thank you very much – john mangual Feb 21 '17 at 23:46
  • As @ReidBarton says, the hackage documentation for `elAttr` states that the second argument should be a `Map String String` . However in the **latest** version of `reflex-dom`, that second argument has been changed to `Map Text Text`. If you installed reflex using 'reflex-platform/try-reflex' per [this answer](http://stackoverflow.com/a/42323953/509928) you should have the `Map Text Text` version. – Dave Compton Feb 22 '17 at 01:51

1 Answers1

0

You may look at the repository https://github.com/hansroland/reflex-dom-inbits.

For an answer to your question see the examples dom03.hs and dom04.hs in the src directory.

The file tutorial.md contains a beginner friendly introduction to reflex-dom and explains the examples. The tutorial is still work in progress.

Jogger
  • 1,617
  • 2
  • 12
  • 22