下面的代码应该产生一个按钮,当按下它时会创建一个框(以及一个用于移除框的按钮)
import Graphics.Input
import Signal (..)
import Signal
import Mouse
import Text (Text, asText, plainText)
import Graphics.Element (..)
import Graphics.Collage (..)
import Color (..)
import List
import Dict
-- x y id | id
type Event = Add (Float, Float, Int) | Remove (Int) | Maybe
makebox : Int -> Element
makebox id =
let (w, h) = (30, 30)
in flow down
[ layers [plainText "aaaa", collage w h [square 30 |> filled red]]
, button_remove id ]
add = Signal.channel (0,0,0)
remove = Signal.channel (0)
button_add x y = Graphics.Input.button (Signal.send add (x, y, 2)) "add a box"
button_remove id = Graphics.Input.button (Signal.send remove (id)) "remove me"
main =
let update event =
case event of
Add (x, y, id) -> Dict.insert id ((x,y), ((makebox id)))
Remove (id) -> Dict.remove id
Maybe -> identity
in Signal.map (\dict -> flow down
[ button_add 10.0 20.0 --makes add & remove buttons
, collage 500 500 (List.map (\(Just ((x,y), makebox)) -> move (x,y) makebox)
(Dict.values dict)) --draws the dict
]) --map function argument
(foldp update Dict.empty
(merge
(Add <~ (Signal.subscribe add)) --pipes button channels into events
(Remove <~ (Signal.subscribe remove)))) --map signal argument
但是,它会产生这种类型的错误:
Type mismatch between the following types on line 40, column 14 to 20:
((Int, Int))
Maybe.Maybe
It is related to the following expression:
update
我看不到这个错误来自哪里, Maybe.Maybe 被传递到哪里update
,我该如何解决?