F#可变列表为null [英] F# mutable list is null
问题描述
当我尝试运行以下代码时,属性为null.为什么会这样??我为属性分配了一个空列表,但是第一次遍历循环时,它为null.
When I try to run the code below, properties is null. Why is that? I assign an empty list to properties, but the first time through the loop, it is null.
这将导致它删除我附加到它的第一个值.我也不明白这一点.似乎一个与null串联的值应该是该值的列表.
This causes it to drop the first value that I append to it. I do not understand this either. It seems that a value concatenated with a null should be list of the value.
[<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答案的解决方案
The solution based on Tarmil's answer
[<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
的原因.
I assume this [<Given>]
attribute comes from a testing framework, which makes me suspect that the assembly is loaded in a non-conventional way by it. The initial value [0]
is set by the module's static constructor, so if this constructor is not run by the testing framework, then that would explain why it stays equal to null
.
这篇关于F#可变列表为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!