0

与えられた:

open System
open System.Linq.Expressions
open Microsoft.FSharp.Quotations
open Microsoft.FSharp.Linq.RuntimeHelpers
open FizzWare.NBuilder

let toLinq (expr: Expr<'a -> 'b>) =
    let linq = LeafExpressionConverter.QuotationToExpression expr
    let call = linq :?> MethodCallExpression
    let lambda = call.Arguments.[0] :?> LambdaExpression
    Expression.Lambda<Func<'a,'b>>(lambda.Body, lambda.Parameters)

let inline with'<'a,'b> (f:Expr<'a->'b>) (value:'b) (operable:IOperable<'a>) = 
    let f = toLinq f
    operable.With(f,value)

let size = 20
    let builderList =
        Builder<dbEncounter.ServiceTypes.Patients>.CreateListOfSize(size).All()
        |> with' <@ fun x -> x.PatientID @> 0
        |> with' <@ fun x -> x.ForeignEHRID @> (Nullable 0)
        |> with' <@ fun x -> x.PatientInfoID @> (Nullable 0)
        |> (fun b -> b.With(fun x-> x.PatientGUID <- Nullable (Guid.NewGuid()); x.PatientGUID ))
        |> withf (fun x-> x.PatientGUID <- Nullable (Guid.NewGuid()); x.PatientGUID) // this line doesn't compile as a replacement for the previous line

私の書き込みの試みwithf:

let inline withf<'a,'b> (f:Func<'a,_>) (operable:IOperable<'a>) = 
operable.With(f)

withf他のオプションを置き換えるために を使用しようとしたときのエラーは

エラー この関数は引数が多すぎるか、関数が想定されていないコンテキストで使用されています

4

1 に答える 1

1

別の質問に対する@kvbの他の回答のおかげで答えが見つかりました

F# と C# ラムダ間の相互運用性

Func次のようにコンストラクターを呼び出すだけです。

let inline withf<'a,'b> (f:'a->'b) (operable:IOperable<'a>) = 
    operable.With(Func<'a,'b>(f))

これでうまくいきます:

let makePatients size = 
    let builderList =
        Builder<dbEncounter.ServiceTypes.Patients>.CreateListOfSize(size).All()
        |> with' <@ fun x -> x.PatientID @> 0
        |> with' <@ fun x -> x.ForeignEHRID @> (Nullable 0)
        |> with' <@ fun x -> x.PatientInfoID @> (Nullable 0)
        //|> (fun b -> b.With(fun x-> x.PatientGUID <- Nullable (Guid.NewGuid()); x.PatientGUID ))
        |> withf (fun x-> x.PatientGUID <- Nullable (Guid.NewGuid()); x.PatientGUID)
于 2015-10-13T16:16:26.157 に答える