Created
December 1, 2018 18:14
-
-
Save grishace/40a55fa46e5f9abda8c1847fc05abfcb to your computer and use it in GitHub Desktop.
?.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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