English 中文(简体)
Why aren t F# records allowed to have AllowNullLiteralAttribute?
原标题:

Is there a compiler implementation reason why records can t have the AllowNullLiteralAttribute attribute or is this a chosen constraint?

I do see this constraint force cleaner code sometimes but not always.

[<AllowNullLiteralAttribute>]
type IBTreeNode = {
    mutable left : IBTreeNode; mutable right : IBTreeNode; mutable value : int}
with
    member this.Insert x =
        if x < this.value then
            if this.left = null then
                this.left <- {left = null; right = null; value = x}
            else
                this.left.Insert x
        else
            if this.right = null then
                this.right <- {left = null; right = null; value = x}
            else
                this.right.Insert x

// Would be a lot of boilerplate if I wanted these public
[<AllowNullLiteralAttribute>]
type CBTreeNode(value) = 
    let mutable left = null
    let mutable right = null
    let mutable value = value
with
    member this.Insert x =
        if x < value then
            if left = null then
                left <- CBTreeNode(x)
            else
                left.Insert x
        else
            if right = null then
                right <- CBTreeNode(x)
            else
                right.Insert x

Added an immutable version for the frown on mutability crowd. It s about 30% faster in this case.

type OBTree =
    | Node of OBTree * OBTree * int
    | Null
with
    member this.Insert x =
        match this with
        | Node(left, right, value) when x <= value -> Node(left.Insert x, right, value)
        | Node(left, right, value) -> Node(left, right.Insert x, value)
        | Null -> Node(Null, Null, x)
问题回答

I can only hazard a guess, but the language seems to take the position that the use of null is something to be avoided (you can always use an option type if you need that functionality), and so the use of null is really something that ought to be limited to interop with other .NET languages. Therefore, F# specific types don t allow the use of null.

Hmm, records are translated to standard .NET-classes like other constructs, so I don t see a technical reason.

I guess the decision is rather philosophical - Null values are uninitialized, undefined values, something that gets replaced within stateful computations. These are to be avoided in functional context like F#.

So I think that record types, as a "functional construct" (not standard .NET-compatible) aren t meant to carry such unfunctional data but the user is required to code it manually (`option types etc.).

This also allows you to reason about your code and e.g. check a pattern matching for all possible values without the potential danger of having to deal with null values.

As to your code: Do you really require it to be that imperative? What about

data Tree = 
    | Node of int * Tree * Tree
    | Nil




相关问题
C#和WPF从媒体记录录像

我的申请将HLSL的沙分效应适用于使用矫正器的媒体。 我如何实时记录和拯救经过修改的录像?

ocaml record type and null

I m trying to define type like: type aaa = NULL | {a: int; b: int};; But the compiler does not allow to do it. I m not sure of the reason why we can t mix record type with anything else. I need to ...

QuickGraph GraphvizRecord doesn t display in vertices

I m trying to build a Graphviz graph containing record vertices using QuickGraph. So far, I have this: var algo = new GraphvizAlgorithm<Entity, EntityEdge>(this); algo.CommonVertexFormat.Shape =...

QSqlTableModel.insertRecord() is very slow

I am using PyQt to insert records into a MySQL database. the code basically looks like self.table = QSqlTableModel() self.table.setTable( mytable ) while True: rec = self.table.record() values = ...

delphi records and c structs

Task: Application written in Delphi accepts a structure (record in terms of Delphi) of three fields. I can send the pointer of this structure using SendMessage (Win32 API) function. So a question is:...

C#: Record and Playback GUI Events

how can i record and playback mouse and keybaord events. i need this to capture the user interactions with my application so that later on i can play to see what user did.

热门标签