c# to f# - F# out parameters and value types -
the following f# function works great if pass references objects, not accept structs, or primitives:
let trygetfromsession (entrytype:entrytype, key, [<out>] outvalue: 't byref) = match httpcontext.current.session.[entrytype.tostring + key] | null -> outvalue <- null; false | result -> outvalue <- result :?> 't; true if try call c# with:
bool result = false; trygetfromsession(theonecache.entrytype.sql,key,out result) i the type bool must reference type in order use parameter there way have f# function handle both?
the problem null value in outvalue <- null restricts type 't reference type. if has null valid value, cannot value type!
you can fix using unchecked.defaultof<'t> instead. same default(t) in c# , returns either null (for reference types) or empty/zero value value types.
let trygetfromsession (entrytype:entrytype, key, [<out>] outvalue: 't byref) = match httpcontext.current.session.[entrytype.tostring() + key] | null -> outvalue <- unchecked.defaultof<'t>; false | result -> outvalue <- result :?> 't; true
Comments
Post a Comment