Dato il seguente:Test per riferimento null in F #
[<DataContract>]
type TweetUser = {
[<field:DataMember(Name="followers_count")>] Followers:int
[<field:DataMember(Name="screen_name")>] Name:string
[<field:DataMember(Name="id_str")>] Id:int
[<field:DataMember(Name="location")>] Location:string}
[<DataContract>]
type Tweet = {
[<field:DataMember(Name="id_str")>] Id:string
[<field:DataMember(Name="text")>] Text:string
[<field:DataMember(Name="retweeted")>] IsRetweeted:bool
[<field:DataMember(Name="created_at")>] DateStr:string
[<field:DataMember(Name="user", IsRequired=false)>] User:TweetUser
[<field:DataMember(Name="sender", IsRequired=false)>] Sender:TweetUser
[<field:DataMember(Name="source")>] Source:string}
deserializzazione con DataContractJsonSerializer(typeof<Tweet[]>)
comporterà l'utente o campo Mittente essere null (almeno questo è quello che il debugger mi sta dicendo).
Se provo a scrivere il seguente:
let name = if tweet.User <> null
then tweet.User.Name
else tweet.Sender.Name
il compilatore emette l'errore: "Il tipo di 'TweetUser' non ha 'null' come un valore corretto"
Come faccio a testare valori null in questo caso?
Vuol 'se tweet.User <> Unchecked.defaultof <_>' funzionano? In caso contrario, c'è sempre l'attributo ['AllowNullLiteral'] (http://msdn.microsoft.com/en-us/library/ee353608.aspx). – ildjarn
Unchecked.defaultof <_> compila ma non funziona in fase di esecuzione (non corrisponde correttamente null). AllowNullLiteral non è valido per un campo record. Buoni suggerimenti comunque. –