为什么我在F#中为字符串获取null?

时间:2015-08-06 21:37:46

标签: f# null

F#上的类型系统的一个好处是避免了一个空的异常...或者我认为这是...因为我得到一个空问题:

[<CLIMutable>]
type Customer = {
    [<AutoIncrement>] id:option<int64>
    code:string
    name:string
}

我正在运行SQL代码:

let SqlFTS<'T>(table:string, searchTable:string, query:string) =
    use db = openDb()
    let sql = sprintf "SELECT * FROM %s WHERE id in (SELECT docid FROM  %s WHERE data MATCH %A)" table searchTable query
    printfn "%A" sql
    db.SqlList<'T>(sql) |> Seq.toArray

testCase "Customers" <| 
    fun _ -> 
        let rows = GenData.genCustomers(50)
        Customers.insert(rows)
        isEqual "Failed to insert" 50L (DB.SqlCount<Customers.Customer>())
        //Until here, it works
        let c = Customers.byId(1L)
        printfn "%A" c
        //Customers.byId return me a record with all the properties as NULLS!
        //Then c.name is null, and the code above fail.
        let rows = Customers.searchCustomers(c.name)

非常意外。为什么我可以获取所有值为null的记录?

1 个答案:

答案 0 :(得分:1)

让我们从第1行开始:

stdout = Popen(callbackquery, shell=True, stdout=PIPE, universal_newlines=True).stdout
output = stdout.read.decode('<encoding>') #The encoding with which the output of the other process is returned, can be something like utf-8, etc.

CLIMutable状态的docs

  

将此属性添加到记录类型会导致它被编译为具有属性getter和setter的默认构造函数的公共语言基础结构(CLI)表示。

默认构造函数意味着字段将初始化为默认值。 string的默认值为null。这对CLR有效,对C#和VB.NET有效。您可能无法从F#调用默认的ctor,但几乎任何其他人都可以。

欢迎互访;这可能是一种痛苦。