Html String testFunc model = div -6ren">
gpt4 book ai didi

elm - 对 "Html msg"感到困惑

转载 作者:行者123 更新时间:2023-12-04 03:25:51 27 4
gpt4 key购买 nike

考虑这个片段:

type alias Model =
{ x : Int }


testFunc : Model -> Html String
testFunc model =
div [] []

我在这里很困惑。 div是一个返回 Html msg 的函数.但是 testFunc返回 Html String .

这是如何编译的?我在这里错过了一些非常基本的理解吗?

最佳答案

你可以善待msg喜欢 Generics in Object Oriented programming .尽管概念相似,但这在技术上并不正确。请记住,严格来说,通用不是正确的术语。

话虽如此,div是一个返回值 Html 的函数具有 msg 的“通用”类型(其中 msg 可以是任何类型)。这将被写为 Html<A>在 Java 或 C# 中,A 在哪里是任何类型的占位符。

榆树有type inference , 所以当返回类型为 testFuncHtml String Elm 推断 msg因此必须是 String类型。

另请注意 Elm 要求泛型类型为小写(例如 msg )。它在 Elm 代码中经常令人困惑,因为人们通常会定义一个真正的类型 Msg并将通用占位符类型命名为 msg .

关于elm - 对 "Html msg"感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41629481/

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