问题
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.
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)
This is very strange.
[Edit] 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)
回答1:
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
.
来源:https://stackoverflow.com/questions/24514966/f-mutable-list-is-null