fsharp报价Expr列表 - > Expr处理

BBL*_*BBL 3 f# quotations

我该怎么做才能让以下工作?

我需要创建一个接受Expr列表并返回Expr(Expr列表 - > Epxr)的函数.

type DataObject() =
    let data = System.Collections.Generic.Dictionary<int, obj>()
    member this.AddValue propertyIndex value = data.Add(propertyIndex, value)
    member this.GetValue propertyIndex = 
        match data.TryGetValue propertyIndex with
        | (true, value) -> value
        | (false, _)    -> box "property not found"

...
(fun args -> 
    <@@ 
        let data = new DataObject(values)
        args |> List.iteri (fun i arg -> data.AddValue i <@@ (%%arg) : string @@>)
        data 
    @@>)
Run Code Online (Sandbox Code Playgroud)

我创建了DataObject类型来添加args的值.但不知怎的,我无法设法让代码迭代不同的args(args.[0] .. args.[i]).我收到的消息是:

变量'arg'绑定在引号中,但用作拼接表达式的一部分.这是不允许的,因为它可能会超出其范围.

如果我显式访问args(args.[0],args.[1],...),解决方案可以工作,但是一旦我尝试添加迭代,我就会遇到问题.因为args列表的长度很灵活,所以对我来说这不是一个可行的解决方案.

我尝试了不同的方法,但不成功.有一些解决方案吗?

[编辑]

在我的解决方案中添加Tomas的反馈给我带来了这个:

type DataObject(values: obj []) =
    let propertyMap = new Map<int, obj>(values |> Seq.mapi (fun i value -> (i, value)))
    member this.GetValue propertyIndex : obj = 
        match propertyMap.TryFind propertyIndex with
        | Some(value) -> value
        | None        -> box "property not found"

(fun args ->  
    let boxedArgs = 
        args |> List.map (fun arg -> 
            match arg with
            | Quotations.Patterns.Var var -> 
                if var.Type = typeof<int> then 
                    <@@ (box (%%arg: int)) @@>
                else if var.Type = typeof<string> then 
                    <@@ (box (%%arg: string)) @@>
                else if var.Type = typeof<System.Guid> then 
                    <@@ (box (%%arg: System.Guid)) @@>
                else 
                    failwith ("Aha: " + var.Type.ToString())
            | _ -> failwith ("Unknown Expr as parameter"))
        <@@ new DataObject(%%(Expr.NewArray(typeof<obj>, boxedArgs))) @@>))
Run Code Online (Sandbox Code Playgroud)

这有效!唯一的问题是我想摆脱if ... else构造以获得正确的转换.有任何想法吗?

Tom*_*cek 7

这是一个棘手的问题!要理解为什么你的代码不起作用,你需要清楚地区分两个级别 - 在一个级别(meta),你正在编写引用,而在另一个级别(base),你正在使用数据对象运行一些代码.

您的代码无法工作的原因是args元级别的表达式列表,您尝试在基础级别迭代它.迭代需要在元级别进行.

解决此问题的一种方法是在元级别进行迭代并生成调用AddValue所有参数的函数列表.然后你可以编写这些函数:

(fun args -> 
    // Given arguments [a0; a1; ...] Generate a list of functions:
    //
    //   [ fun data -> data.AddValue 0 a0; data ]
    //   [ fun data -> data.AddValue 1 a1; data ... ]
    args 
    |> List.mapi (fun i arg -> 
      <@ fun (data:DataObject) -> data.AddValue i (%%arg : string); data @>)

    // Compose all the functions just by calling them - note that the above functions 
    // take DataObject, mutate it and then return it. Given [f0; f1; ...] produce:
    //
    //    ... (f1 (f0 (new DataObject())))
    //
    |> List.fold (fun dobj fe -> <@ (%fe) (%dobj) @>) <@ new DataObject() @> )
Run Code Online (Sandbox Code Playgroud)

这写起来很有趣,但它变得非常复杂.在实践中,您可以通过向AddValues数据对象添加方法(获取obj[])并使用Expr.NewArray创建包含所有参数值(从元级别)的单个数组(在基础级别)来使事情变得更容易:

<@@ let d = new DataObject()
    d.AddValues(%(Expr.NewArray(typeof<obj>, args)))
    d @@>
Run Code Online (Sandbox Code Playgroud)