Home
Insert
Update snippet 'Preempting NullReferenceExceptions'
Title
Passcode
Description
When interfacing .NET libraries that potentially yield null values, a lot of boilerplate code is required to ensure no NullReferenceExceptions are raised. This is an attempt to avoid such repetition with computation expressions.
Source code
/// strips values of reference types that are null let strip<'T when 'T : null> (x : 'T) = match x with null -> None | x -> Some x type DenullBuilder() = member __.Return x = x member __.Zero() = null member __.Bind(x, f) = match x with null -> null | x -> f x let denull = new DenullBuilder() // example open Microsoft.Win32 denull { let bkey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Default) let! k1 = bkey.OpenSubKey(@"Software\Microsoft\VisualStudio\11.0") let! k2 = k1.OpenSubKey(@"DialogPage\Microsoft.VisualStudio.FSharp.Interactive.FsiPropertyPage") let! switch = k2.GetValue "FsiPreferAnyCPUVersion" :?> string if switch = "True" then return k2.GetValue "FsiCommandLineArgs" :?> string } |> strip
Tags
nullable types
computation expressions
nullable types
computation expressions
Author
Link
Reference NuGet packages
If your snippet has external dependencies, enter the names of NuGet packages to reference, separated by a comma (
#r
directives are not required).
Update