F#方法返回null而不是Option

bur*_*huy 6 .net f# .net-4.6.1

我开发F#的应用程序.net 4.6.1VS2015.我有方法:

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
Run Code Online (Sandbox Code Playgroud)

...

    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)) 
Run Code Online (Sandbox Code Playgroud)

但是,当我称这种方法时,我接受null而不是option. 在此输入图像描述 我做错了什么?

Fyo*_*kin 11

在F#中,可以null在运行时使用常量表示DU的一个无数据值.您可以通过以下方式指示编译器执行此操作CompilationRepresentationFlags.UseNullAsTrueValue:

[<CompilationRepresentation(CompilationRepresentationFlags.UseNullAsTrueValue)>]
type A = B | C of int

printfn "%A" (obj.ReferenceEquals( B, null ))  // will print "true"
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,DU值B被编译为null.这对于优化目的来说有时很好:不是每次都分配一个实例,而是使用常量.帮助是否使用了很多值.

所以Option类型使用了同样的技术None的情况下,这就是为什么None了作为显示null在调试器.

有一天,调试器将有适当的扩展点来实现这个和其他F#功能.在那之前,调试器会说C#,你可以进行翻译.

  • 调试器在VS2015之前工作正常,发生了什么? (2认同)