I develop F#
application .net 4.6.1
on VS2015
. i have methods:
type CommonHelper =
static member SideEffectOnNull act x = if x = null then act(); x else x
static member SideEffectOnNotNull act x = if x <> null then act(); x else x
...
static member GetPerformanceCounter ( fname: CounterFullName ) =
let getCounterInternal ( counterFullName: CounterFullName ) =
Log.Information("Getting counter: {category}\\{name}\\{instance} ", counterFullName.Category, counterFullName.Name, counterFullName.Instance)
let receivedCategory = PerformanceCounterCategory.GetCategories().FirstOrDefault( fun x -> String.Equals( x.CategoryName, counterFullName.Category.Category, StringComparison.InvariantCultureIgnoreCase ) )
if receivedCategory = null then
Serilog.Log.Warning ( "Category not found: {category}", counterFullName.Category ); null
else
let receivedCounters = PerforrmanceCounterProxy.GetPerformanceCountersFromCategoryOrNull counterFullName.Instance receivedCategory
if receivedCounters = null then
Log.Warning ("Instance not found {name}(instance: {instance}) in category {category}", counterFullName.Name, counterFullName.Instance, counterFullName.Category); null
else
receivedCounters.FirstOrDefault( fun y -> String.Equals( y.CounterName, counterFullName.Name.Name, StringComparison.InvariantCultureIgnoreCase ) )
|> CommonHelper.SideEffectOnNull ( fun unit -> Log.Warning ("Name {name}(instance: {instance}) not found for category {category}", counterFullName.Name, counterFullName.Instance, counterFullName.Category) )
getCounterInternal fname
|> CommonHelper.SideEffectOnNull (fun unit ->Log.Warning( "Getting counter failed: {category}\\{name}\\{instance}", fname.Category, fname.Name, fname.Instance ))
|> CommonHelper.SideEffectOnNotNull (fun unit ->Log.Information( "Getting Counter secceed: {category}\\{name}\\{instance}", fname.Category, fname.Name, fname.Instance ))
|> (fun x -> if x = null then None else Option.Some(x))
But when i call this method i receive null
instead of option
.
What i'm doing wrong?
In F# it is possible to represent one data-less value of a DU with the null
constant at runtime. You can instruct the compiler to do so with CompilationRepresentationFlags.UseNullAsTrueValue
:
[<CompilationRepresentation(CompilationRepresentationFlags.UseNullAsTrueValue)>]
type A = B | C of int
printfn "%A" (obj.ReferenceEquals( B, null )) // will print "true"
In the above code, the DU value B
gets compiled to null
. This is sometimes nice for optimization purposes: instead of allocating an instance every time, I use a constant. Helps if the value is used a lot.
So the Option
type uses this same technique for the None
case, and that's why None
shows up as null
in the debugger.
One day, the debugger will have appropriate extension points to implement this and other F# features. Until then, the debugger speaks C#, and you get to do the translation.
来源:https://stackoverflow.com/questions/41337310/f-method-returns-null-instead-of-option