Last active
December 18, 2015 20:38
-
-
Save jbevain/5841252 to your computer and use it in GitHub Desktop.
This file contains 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 System.Reflection | |
open Microsoft.FSharp.Quotations | |
open Microsoft.FSharp.Quotations.Patterns | |
[<ReflectedDefinition>] | |
let id x = x | |
[<ReflectedDefinition>] | |
let mul x y = x * y | |
let methodof (e: Expr) = | |
let rec extract_method = function | |
| Lambda (_, b) -> extract_method (b) | |
| Call (_, m, _) -> m | |
| _ -> failwith "frak" | |
e |> extract_method | |
let propertyof = function | |
| PropertyGet (_, p, _) -> p | |
| _ -> failwith "frak" | |
let fieldof = function | |
| FieldGet (_, f) -> f | |
| _ -> failwith "frak" | |
let code_of (m: MethodBase) = | |
match Expr.TryGetReflectedDefinition (m) with | |
| Some (e) -> e.ToString () | |
| _ -> failwith "frak" | |
methodof (<@@ id @@>) |> code_of |> Console.WriteLine | |
methodof (<@@ mul @@>) |> code_of |> Console.WriteLine | |
type Person (name: string) = | |
[<ReflectedDefinition>] | |
member self.LowerName () = | |
name.ToLower () | |
member self.NameField () = | |
fieldof (<@@ name @@>) | |
let p = Person ("JbEvain") | |
methodof (<@@ p.LowerName @@>) |> code_of |> Console.WriteLine | |
Console.WriteLine (id.GetType ()) | |
Console.WriteLine (mul.GetType ()) | |
Console.WriteLine (p.LowerName ()) | |
let mutable xyz = "foo" | |
Console.WriteLine (xyz) | |
xyz <- "bar" | |
Console.WriteLine (xyz) | |
propertyof (<@@ p @@>) |> Console.WriteLine | |
propertyof (<@@ xyz @@>) |> Console.WriteLine | |
Console.WriteLine (p.NameField ()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment