gpt4 book ai didi

elm - 编译器不强制指定参数类型 - 这是错误还是预期?

转载 作者:行者123 更新时间:2023-12-04 15:52:13 25 4
gpt4 key购买 nike

我已经看到这段代码编译没有错误,我不能说是错误还是预期。

type alias Foo = List 
vs
type alias Foo = List String

它不仅与 List .也允许自定义联合类型。前任:
type State value = Valid value | Invalid value

type alias Model1 =
{ someField : State String } -- i would say this is normal. State is a string..

type alias Model2 =
{ someField : State } -- this doesn't look right.

并且还允许功能
function1 : List String -> Int
function1 aListOfStrings =
1

function2 : List -> Int
function2 whatisThisNow =
1

但如果是预期的 - 如何推理呢?我想不通。玩它 here .

最佳答案

第一个看起来还行。定义 type alias Foo = List应该允许您使用 Foo而不是 List .但它不能编译(使用 Elm 0.18):

type alias Foo = List 

names : Foo String --does not compile
names = ["a", "b"]

声明时似乎没有完全检查类型别名,因此可以创建根本无法使用的类型别名。

对于第一个示例,可以修复编译器以正确支持它。但是,第二个示例应该是编译时错误,因为无法获得类型 List 的值。 (或 State)。 Haskellers 会说 List (或 State )有种类 * -> * ,但运行时的值只能有种类 * .

我猜你在当前的 Elm 版本 (0.18) 中发现了一个错误

有趣的是,将上面的代码更改为

type alias Foo a = List a

names : Foo String
names = ["a", "b"]
-- compiles with Elm 0.18

使其正常工作。不过,这两个代码片段应该是等效的。

关于elm - 编译器不强制指定参数类型 - 这是错误还是预期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44228017/

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