根据返回类型,Signal(Int,Int)不会被识别为(Int,Int)的元组.
请考虑以下代码:
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
Run Code Online (Sandbox Code Playgroud)
Signal.map relativeMouse Mouse.position 工作正常,显示(-1000,-1000)到浏览器,并根据鼠标移动调整值.
relativeMouseTuple Mouse.position这虽然不起作用.我得到的复杂错误如下:
Function `relativeMouseTuple` is expecting the argument to be:
( Int, Int )
But it is:
Signal ( Int, Int )
Run Code Online (Sandbox Code Playgroud)
我觉得这很奇怪.在这两种情况下,第一个参数是Signal(Int,Int),在第二种情况下,它导致类型错误.
有任何想法吗?
我没有榆树的经验,但是:
Signal.map将函数映射到信号,这并不意味着使用Signalas作为参数调用函数,而是使用signal的参数作为参数调用它.例如,为Signal(Int, Int)您映射一个(Int, Int)作为参数接收的函数.
所以在下面的例子中你没有问题
Signal.map relativeMouse Mouse.position
Run Code Online (Sandbox Code Playgroud)
但是,在下面的情况中,您调用一个期望(Int, Int)参数Signal(Int, Int)错误的函数:
relativeMouseTuple Mouse.position
Run Code Online (Sandbox Code Playgroud)
您应该做的,可能是将您的函数映射到Signal,如下所示:
Signal.map relativeMouseTuple Mouse.position
Run Code Online (Sandbox Code Playgroud)