msg) -> Cmd a -> Cmd -6ren">
gpt4 book ai didi

Elm 文档 - "a"是什么意思?

转载 作者:行者123 更新时间:2023-12-04 17:07:25 30 4
gpt4 key购买 nike

Elm 文档没有给出示例用法,因此很难理解类型规范的含义。在几个地方,我看到“a”用作参数标识符,例如 Platform.Cmd :

map : (a -> msg) -> Cmd a -> Cmd msg

这是什么意思?我将如何使用 Cmd.map ?

最佳答案

这是 Elm Guide about reading Types Annotations 的一部分:

> [ "Alice", "Bob" ]
[ "Alice", "Bob" ] : List String

> [ 1.0, 8.6, 42.1 ]
[ 1.0, 8.6, 42.1 ] : List Float

> []
[] : List a

In the first case, we have a List filled with String values. In the second, the List is filled with Float values. In the third case the list is empty, so we do not actually know what kind of values are in the list. So the type List a is saying "I know I have a list, but it could be filled with anything". The lower-case a is called a type variable, meaning that there are no constraints in our program that pin this down to some specific type. In other words, the type can vary based on how it is used.



在您的情况下,作为第一个参数,您应该传递一个函数,该函数将从第二个参数中获取类型变量并返回 msg 类型的值。

关于Elm 文档 - "a"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42703224/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com