gpt4 book ai didi

arrays - F# - 定义对象数组

转载 作者:行者123 更新时间:2023-12-02 22:18:58 25 4
gpt4 key购买 nike

我正在尝试为我正在做的项目创建一些示例数据。为此,我需要一个简单的对象结构,其中 on objects 包含一个对象数组。在这种情况下,可以进行评论的评论家。代码如下所示:

type Review = 
{ MovieName: string;
Rating: double }

type Critic =
{ Name: string;
Ratings: Review[] }

let theReviews = [{ MovieName = "First Movie"; Rating = 5.5 }; { MovieName = "Second Movie"; Rating = 7.5 };]

let theCritic = { Name = "The Critic"; Ratings = [{ MovieName = "First Movie"; Rating = 5.5 }; { MovieName = "Second Movie"; Rating = 7.5 };] }

“评论”行工作正常,但我无法让“theCritict”行工作。我得到的错误是:

Error 1 This expression was expected to have type Review []
but here has type 'a list

我如何让它工作?

最佳答案

数组文字使用 [| ... |] 而不是 [ ... ] 是列表文字 - 你需要类似的东西

let theCritic = { Name = "The Critic"; Ratings = [|{ MovieName = "First Movie"; Rating = 5.5 }; { MovieName = "Second Movie"; Rating = 7.5 };|]  }

这解释了错误 - 你使用了列表文字而不是数组文字

关于arrays - F# - 定义对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14015630/

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