F# mutable list is null

梦想与她 提交于 2019-12-10 16:55:47

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!