Search code examples
domelm

Get Int value out of Signal Int


I am facing issue in taking Int value out of Window.dimensions's return val - Signal Int.

view : Signal.Address Action -> Model -> Html
view address model =
  let wx = Signal.map fst Window.dimensions 
      wy = Signal.map snd Window.dimensions  
  in fromElement <| container wx wy middle <| toElement  100 100 <|
      div []
        [ button [ onClick address Decrement ] [text "-"]
        ]

This line, wx = Signal.map fst Window.dimensions to get window container x-coordinates throws error as,

Type mismatch between the following types on line 30, column 31 to 33:
        Signal.Signal In
        Int
    It is related to the following expression:
        wx

Solution

  • Window.dimensions is a Signal (Int, Int). Mapping over it with fst or snd will give you a Signal Int which you can't just get "the value out of".

    Your view shouldn't have Signal in it at all. It should just take some state in and return some Html. If you want to make a container that is the same dimensions as the screen, better to have your view function take the width/height and then map your view function over Window.dimensions. You can use this as an example.