0 people like it.

Quotation property name + comp + value extract

for zec_ on irc

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
open Microsoft.FSharp.Quotations

module P = Quotations.Patterns

type Comparison
  = Equals of string * obj
  | NotEquals of string * obj

let getNameValuePair (expr:Expr<('a -> bool)>) =
  match expr with
  | P.Lambda(_, P.Call(None, mi, [Patterns.PropertyGet(_, pi, []); P.Value(value, _) ])) -> 

    match mi.Name with
    | "op_Equality" ->Some(Equals(pi.Name, value))
    | "op_Inequality" -> Some(NotEquals(pi.Name, value))
    | _ -> None

  | _ -> None
  
let test = <@ fun (s:string) -> s.Length <> 2 @>

let r = getNameValuePair test
namespace Microsoft
namespace Microsoft.FSharp
namespace Microsoft.FSharp.Quotations
module Patterns

from Microsoft.FSharp.Quotations
type Comparison =
  | Equals of string * obj
  | NotEquals of string * obj

Full name: Script.Comparison
union case Comparison.Equals: string * obj -> Comparison
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
type obj = System.Object

Full name: Microsoft.FSharp.Core.obj
union case Comparison.NotEquals: string * obj -> Comparison
val getNameValuePair : expr:Expr<('a -> bool)> -> Comparison option

Full name: Script.getNameValuePair
val expr : Expr<('a -> bool)>
Multiple items
type Expr =
  override Equals : obj:obj -> bool
  member GetFreeVars : unit -> seq<Var>
  member Substitute : substitution:(Var -> Expr option) -> Expr
  member ToString : full:bool -> string
  member CustomAttributes : Expr list
  member Type : Type
  static member AddressOf : target:Expr -> Expr
  static member AddressSet : target:Expr * value:Expr -> Expr
  static member Application : functionExpr:Expr * argument:Expr -> Expr
  static member Applications : functionExpr:Expr * arguments:Expr list list -> Expr
  ...

Full name: Microsoft.FSharp.Quotations.Expr

--------------------
type Expr<'T> =
  inherit Expr
  member Raw : Expr

Full name: Microsoft.FSharp.Quotations.Expr<_>
type bool = System.Boolean

Full name: Microsoft.FSharp.Core.bool
active recognizer Lambda: Expr -> (Var * Expr) option

Full name: Microsoft.FSharp.Quotations.Patterns.( |Lambda|_| )
active recognizer Call: Expr -> (Expr option * System.Reflection.MethodInfo * Expr list) option

Full name: Microsoft.FSharp.Quotations.Patterns.( |Call|_| )
union case Option.None: Option<'T>
val mi : System.Reflection.MethodInfo
active recognizer PropertyGet: Expr -> (Expr option * System.Reflection.PropertyInfo * Expr list) option

Full name: Microsoft.FSharp.Quotations.Patterns.( |PropertyGet|_| )
val pi : System.Reflection.PropertyInfo
active recognizer Value: Expr -> (obj * System.Type) option

Full name: Microsoft.FSharp.Quotations.Patterns.( |Value|_| )
val value : obj
property System.Reflection.MemberInfo.Name: string
union case Option.Some: Value: 'T -> Option<'T>
val test : Expr<(string -> bool)>

Full name: Script.test
val s : string
property System.String.Length: int
val r : Comparison option

Full name: Script.r
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/5J
Posted:13 years ago
Author:fholm
Tags: qoutations