Skip to content

Instantly share code, notes, and snippets.

@grishace
Created December 1, 2018 18:14
Show Gist options
  • Select an option

  • Save grishace/40a55fa46e5f9abda8c1847fc05abfcb to your computer and use it in GitHub Desktop.

Select an option

Save grishace/40a55fa46e5f9abda8c1847fc05abfcb to your computer and use it in GitHub Desktop.
?.
open System
open Microsoft.FSharp.Quotations
open FSharp.Quotations.Evaluator
type A (str: string) =
member val Prop: string = str with get, set
let inline safeGet (f: Expr<'T>): 'T option =
try Some(f.Evaluate())
with :? NullReferenceException -> None
[<EntryPoint>]
let main argv =
let a = A(null)
printfn "%A" (safeGet <@ a.Prop.Length @>)
let b = A("123")
printfn "%A" (safeGet <@ b.Prop.Length @>)
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment