Tworzę aplikację F#
.net 4.6.1
pod numerem VS2015
. Mam metody:Metoda F # zwraca wartość null zamiast opcji
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))
Ale kiedy wywołanie tej metody i otrzymać null
zamiast option
. Co robię źle?
Moje przypuszczenie - problem związany z 'visual studio 2015'. Ponieważ logika biznesowa działa dobrze, myślę, że interfejs użytkownika "VS2015" pokazuje niepoprawne informacje – burzhuy
Miałem domyślną wartość dla tego typu pojawia się podczas przechodzenia - "domyślnie (T)" w przypadku typów wartości i "null" dla typów odniesienia. – Asti