【问题标题】:Signal (Int, Int) not recognised as (Int, Int) tuple in some cases在某些情况下,信号 (Int, Int) 未被识别为 (Int, Int) 元组
【发布时间】:2015-12-10 13:27:32
【问题描述】:

根据返回类型,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

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 )

我觉得这很奇怪。在这两种情况下,第一个参数都是 Signal (Int, Int),而在第二种情况下,它会导致类型错误。

有什么想法吗?

【问题讨论】:

    标签: elm elm-signal


    【解决方案1】:

    但是,我完全没有使用榆树的经验:

    Signal.map 将函数映射到信号,这并不意味着以Signal 作为参数调用函数,而是以信号的参数作为参数调用它。例如对于Signal(Int, Int),您映射一个接收(Int, Int) 作为参数的函数。

    所以在以下情况下你没有问题

    Signal.map relativeMouse Mouse.position
    

    但是,在下面的例子中,你调用了一个期望 (Int, Int) 的函数,但参数 Signal(Int, Int) 是错误的:

    relativeMouseTuple Mouse.position
    

    你应该做的,可能是将你的函数映射到这样的信号:

    Signal.map relativeMouseTuple Mouse.position
    

    【讨论】:

    • 对于没有 Elm 经验的人来说,这是一个很好的答案 :)
    • @MByD 这是map : (a -> result) -> Signal a -> Signal result 的定义,与您在回答中描述的内容一致。
    猜你喜欢
    • 2020-11-05
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    • 1970-01-01
    • 2018-10-28
    • 2014-08-27
    • 2017-02-23
    • 2018-11-16
    相关资源
    最近更新 更多