我在F#
上开发了.net 4.6.1
申请VS2015
。我有方法:
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))
答案 0 :(得分:10)
在F#中,可以在运行时表示具有null
常量的DU的一个无数据值。您可以使用CompilationRepresentationFlags.UseNullAsTrueValue
指示编译器执行此操作:
[<CompilationRepresentation(CompilationRepresentationFlags.UseNullAsTrueValue)>]
type A = B | C of int
printfn "%A" (obj.ReferenceEquals( B, null )) // will print "true"
在上面的代码中,DU值B
被编译为null
。这对于优化目的来说有时很好:不是每次都分配一个实例,而是使用常量。如果价值被大量使用,则有助于此。
因此,Option
类型对None
案例使用了相同的技术,这就是None
在调试器中显示为null
的原因。
有一天,调试器将有适当的扩展点来实现这个和其他F#功能。在那之前,调试器会说C#,你可以进行翻译。