gpt4 book ai didi

F# 可变列表为空

转载 作者:行者123 更新时间:2023-12-01 01:16:48 25 4
gpt4 key购买 nike

当我尝试运行下面的代码时,属性为空。这是为什么? 我为属性分配了一个空列表,但第一次通过循环时,它为空。

这会导致它删除我附加到它的第一个值。我也不明白这一点。似乎与 null 连接的值应该是该值的列表。

[<TechTalk.SpecFlow.Binding>]
module FSharpLeftistHeapStepDefinition

open TechTalk.SpecFlow

let mutable properties = [0]
let [<Given>] ``I have a list with the following numbers``(values:Table) =
let rec insertItems i =
if i < 0 then ()
else
let value = int ((values.Rows.Item i).Item 0)
properties <- value::properties
insertItems (i-1)

insertItems (values.RowCount - 1)

这很奇怪。

[编辑]
基于 Tarmil 的答案的解决方案
 [<Binding>]
type Example ()=
let mutable properties = []
let [<Given>] ``I have a list with the following numbers``(values:Table) =
let rec insertItems i =
if i < 0 then ()
else
let value = int ((values.Rows.Item i).Item 0)
properties <- value::properties
insertItems (i-1)

insertItems (values.RowCount - 1)

最佳答案

我假设这 [<Given>]属性来自一个测试框架,这让我怀疑程序集是由它以非常规方式加载的。初始值[0]由模块的静态构造函数设置,所以如果这个构造函数不是由测试框架运行,那么这就解释了为什么它保持等于 null .

关于F# 可变列表为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24514966/

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