1

Depending on the return type, Signal (Int, Int) is not recognised as a tuple of (Int, Int).

Consider the following code:

import Graphics.Element exposing (..)
import Mouse


relativeMouseElement : (Int, Int) -> Element
relativeMouseElement mp = show (fst mp - 1000, snd mp - 1000)


relativeMouseTuple : (Int, Int) -> (Int, Int)
relativeMouseTuple mp = (fst mp - 1000, snd mp - 1000)

main =
--  Signal.map relativeMouse Mouse.position
    relativeMouseTuple Mouse.position

Signal.map relativeMouse Mouse.position works just fine, displays (-1000, -1000) to the browser and the values are adjusted according to the mouse movement.

relativeMouseTuple Mouse.position This though does not work. The complication error I get is the following:

Function `relativeMouseTuple` is expecting the argument to be:

    ( Int, Int )

But it is:

    Signal ( Int, Int )

I find this very weird. In both case the first argument is Signal (Int, Int) and in the second case it results to a type error.

Any ideas?

skay-
  • 1,546
  • 3
  • 16
  • 26

1 Answers1

5

I have no experience with elm AT ALL, however:

Signal.map maps a function to a signal, this doesn't mean that the function is called with the Signal as a parameter, but that it is called with the signal's arguments as parameter. e.g. for Signal(Int, Int) you map a function that receives (Int, Int) as arguments.

So in the following case you have no problem

Signal.map relativeMouse Mouse.position

However, in the case below, you call a function that expects (Int, Int) with the argument Signal(Int, Int) which is wrong:

relativeMouseTuple Mouse.position

What you should do, is probably map your function to the Signal like this:

Signal.map relativeMouseTuple Mouse.position
Apanatshka
  • 5,958
  • 27
  • 38
MByD
  • 135,866
  • 28
  • 264
  • 277
  • That's a great answer for someone who has no experience with Elm :) – Apanatshka Dec 10 '15 at 13:51
  • @MByD This is the definition of `map : (a -> result) -> Signal a -> Signal result` which is spot on with what you described as part of your answer. – skay- Dec 10 '15 at 14:09