Testing for null reference in F#

后端 未结 3 853
渐次进展
渐次进展 2020-12-17 15:56

Given the following:

[]
type TweetUser = {
    [] Followers:int
    [

        
相关标签:
3条回答
  • 2020-12-17 16:33

    To cyclically expand on @Tomas' answer ;-]

    let name = if not <| obj.ReferenceEquals (tweet.User, null)
                  then tweet.User.Name
                  else tweet.Sender.Name
    

    or

    let inline isNull (x:^T when ^T : not struct) = obj.ReferenceEquals (x, null)
    

    Unchecked.defaultof<_> is doing the right thing and producing nulls for your record types; the issue is that the default equality operator uses generic structural comparison, which expects you to always play by F#'s rules when using F# types. In any case, a null-check really only warrants referential comparison in the first place.

    0 讨论(0)
  • 2020-12-17 16:35

    To add some details to the comment by @ildjarn, you are getting the error message, because F# does not allow using null as a value of types that are declared in F#. The motivation for this is that F# tries to eliminate null values (and NullReferenceException) from pure F# programs.

    However, if you're using types that are not defined in F#, you are still allowed to use null (e.g. when calling a function that takes System.Random as an argument, you can give it null). This is needed for interoperability, because you may need to pass null to a .NET library or accept it as a result.

    In your example, TweetUser is a (record) type declared in F#, so the language does not allow treating null as a value of type TweetUser. However, you can still get null value through i.e. Reflection or from C# code, so F# provides an "unsafe" function that creates a null value of any type - including F# records, which should not normally have null value. This is the Unchecked.defaultOf<_> function and you can use it to implement a helper like this:

    let inline isNull x = x = Unchecked.defaultof<_>
    

    Alternatively, if you mark a type with the AllowNullLiteral attribute, then you're saying to the F# compiler that it should allow null as a value for that specific type, even if it is a type declared in F# (and it would not normally allow null).

    0 讨论(0)
  • 2020-12-17 16:42

    Though this question is old, I didn't see any examples of boxing to solve the problem. In cases where my presenter does not allow null literals, but may be set from a view, I prefer to use boxing.

    isNull <| box obj
    

    or

    let isMyObjNull = isNull <| box obj
    

    or

    match box obj with
    | null -> (* obj is null *)
    | _ -> (* obj is not null *)
    
    0 讨论(0)
提交回复
热议问题