如何在Elm 0.13中使用字段 [英] How to use Fields in Elm 0.13
问题描述
我一直在努力使这些领域发挥作用,但一直失败。我也一直在尝试寻找示例,但我能找到的唯一示例是使用Elm 0.14,该示例使用的新Channel API在Elm 0.13中不可用。
I have been trying to get the fields to work, but keep failing. I have also been trying to look for examples, but the only examples I could find are using Elm 0.14, which use the new Channel API which isn't available in Elm 0.13.
因此,我从中提供的示例开始目录
import Graphics.Input.Field (..)
import Graphics.Input (..)
name : Input Content
name = input noContent
nameField : Signal Element
nameField = field defaultStyle name.handle identity "Name" <~ name.signal
为了使用该字段,我尝试了
And in order to use the field I tried
main : Signal Element
main = Signal.lift2 display Window.dimensions gameState
display : (Int,Int) -> GameState -> Element
display (w,h) g =
container w h middle <|
collage gameWidth gameHeight
(if | g.state == Menu ->
[ rect gameWidth gameHeight
|> filled black
, toForm nameField
, plainText "*The name entered in the nameField*"
]
| otherwise -> []
)
但是我一直收到以下错误
But I keep getting the following error
Expected Type: Signal.Signal Graphics.Element.Element
Actual Type: Graphics.Element.Element
为什么不该元素不再是信号...函数定义明确指出它应该输出信号,对吧?现在如何输入名称,然后可以在变量内使用?
Why isn't the element a signal anymore... The function definition clearly states it should output a signal, right? Now how would I be able to enter a name, that I would then be able to use inside a variable?
推荐答案
Elm 0.13有一些令人讨厌的类型错误消息。通常将预期/实际交换。在这种情况下,问题出在 display:(Int,Int)->中使用
。 nameField:Signal Element
。 GameState->元素 display
是纯(非信号)功能,但要纯正,您不能在其中的任何地方使用信号。为了解决这个问题,将 nameField
信号提升一个水平,到 main
。要使用在该字段中输入的内容,请使用输入信号:
Elm 0.13 had some annoyingly confusing type error messages. Expected/Actual are usually swapped. In this case the problem comes from using nameField : Signal Element
in display : (Int,Int) -> GameState -> Element
. display
is a pure (non-signal) function, but to be pure, you can't use a signal anywhere in there. To solve this, hoist the nameField
signal up a level, to main
. To use what is entered in the field, use the input signal:
main : Signal Element
main = Signal.lift4 display Window.dimensions gameState name.signal
nameField : Content -> Element
nameField = field defaultStyle name.handle identity "Name"
display : (Int,Int) -> GameState -> Content -> Element
display (w,h) g currentContent =
container w h middle <|
collage gameWidth gameHeight
(if | g.state == Menu ->
[ rect gameWidth gameHeight
|> filled black
, toForm (nameField currentContent) -- use something other than `currentContent` here to influence the field content.
, plainText currentContent.string
]
| otherwise -> []
)
这篇关于如何在Elm 0.13中使用字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!