hubFS: THE place for F#

. . . are you on The Hub?
Welcome to hubFS: THE place for F# Sign in | Join | Help
in Search

Null values for F# classes

Last post 05-20-2008, 3:37 by dsyme. 2 replies.
Sort Posts: Previous Next
  •  05-19-2008, 14:50 5957

    Null values for F# classes

    I'm trying to make our object/relational mapping library work with F#. The use of this library involves deriving "persistent" classes from the library base classes, decorating them with certain attributes and accessing certain base class functionality.

    The persistent classes can have members (field/property combinations) that reference other persistent classes. For the persistence mechanism it's important that on instantiation of the class, the fields are set to standard values - in the case of references to other classes, the expected default value is null.

    I read a bunch of threads on the hub about using null values. I briefly tried using option values instead of null, but at the very least this would require changes to our library - the generic Option<T> is simply not recognized as being a persistable type. Next I tried this suggestion:

    let mynull : 'a when 'a : not struct = (# "ldnull" : 'a #)

    This doesn't seem to work (and the compiler complains about the deprecated inline IL anyway). I have a class like this:

    type Blah(session:Session) =
        inherit XPObject(session) as base
        
        let mutable artist : Artist = mynull

    This gives me this warning: "warning FS0064: This construct causes code to be less generic than indicated by the type annotations. The type variable 'a has been constrained to be type 'Artist'." Then, when I add an additional line:

       let mutable album: Album = mynull

    ... it starts giving me "Error 3 This expression has type  Artist but is here used with type  Album", quite obviously. I have tried assigning (mynull :> Artist) and (mynull :> Album), but that just makes type inference go wrong in some other way. Perhaps there's a way to annotate this in such a way that it works? It seems weird that mynull is explicitly generic, but type inference can't deal with it automatically.

    What other way is there for me to use? I've seen several comments by Don about a request for an attribute that would allow annotating a class to say "I want to be able to use null with this". I'm not entirely clear why this would be a class-based annotation (as opposed to an annotation on the variable), but in any case this sounds really useful for interop scenarios like mine. Are there any news about this feature?

    Thanks

    Oliver

  •  05-19-2008, 15:43 5958 in reply to 5957

    Re: Null values for F# classes

    Okay, please disregard this post - I found the answer to my question in the release notes for F# 1.9.4: use the Unchecked.defaultof<T> function. Great, that works for me!

    I guess that the deprecation of previous patterns, as also described in that post, explains why the other code snippet wasn't working for me.

  •  05-20-2008, 3:37 5963 in reply to 5958

    Re: Null values for F# classes

    Hi Oliver,

    Thanks for posting the follow up - yes, Unchecked.defaultof<_> is exactly for use in this kind of situation.

    Kind regards

    don

     

View as RSS news feed in XML
Powered by Community Server, by Telligent Systems