在列表中收集 TypeProvider ProvidedMethod 参数值
Collect TypeProvider ProvidedMethod argument values in list
这是 Provided 方法的一个简化片段,它接受可变数量的参数,在本例中为 3
ProvidedMethod(methodName = "GetContext",
parameters = [ for i in [ 1..3 ] do
yield ProvidedParameter("Param" + string i, typeof<string>) ],
IsStaticMethod = true, returnType = typeof<string>,
InvokeCode = (fun args ->
<@@
let dim1 : string = %%args.[0] : string
let dim2 : string = %%args.[1] : string
let dim3 : string = %%args.[2] : string
// let dims = [for %%arg in args do yield (arg : string) ]// [1] error below
// let dims = [for arg in args do yield (%%arg : string) ]// [2] error below
let dims = [ dim1; dim2; dim3 ] //this works
String.Join("--", dims)
@@>))
我想在一个列表中收集所有参数。
我尝试过但没有奏效的内容在代码引用中进行了注释。
[1]: [FS0010] Unexpected prefix operator in expression
[FS0594] Identifier expected
[2]: [FS0446] The variable 'arg' is bound in a quotation but is used as part of a spliced expression. This is not permitted since it may escape its scope.
按照以下方式破解您的解决方案实际上可以编译
InvokeCode = (fun args ->
let dims: string[] = Array.zeroCreate args.Length
let mutable i = 0
let inc () = i <- i + 1
<@@
while i < args.Length do
dims.[i] <- %%args.[i]
inc ()
String.Join("--", dims)
@@>
但我怀疑您更想将形状 [|Value ("a"); Value ("b"); Value ("c")|]
的 Quotations.Expr[]
转换为单个 Quotations.Expr
.
您可以使用 Microsoft.FSharp.Quotations.Patterns
中的模式按以下方式从表达式中提取内容
InvokeCode = (fun args ->
let dims =
args
|> Array.choose (function | Value(value, _) -> value |> string |> Some | _ -> None)
|> fun arr -> String.Join("--", arr)
<@@ dims @@>
这种解决方案也基于评论中建议的答案起作用:
ProvidedMethod(methodName = "GetContext",
parameters = [ for i in [ 1..3 ] do
yield ProvidedParameter("Param" + string i, typeof<string>) ],
IsStaticMethod = true, returnType = typeof<string>,
InvokeCode = (fun args ->
let dims = List.fold ( fun state e -> <@@ (%%string)::%%state @@>) <@@ []:List<string> @@> args
<@@
String.Join("--", dims)
@@>))
这是 Provided 方法的一个简化片段,它接受可变数量的参数,在本例中为 3
ProvidedMethod(methodName = "GetContext",
parameters = [ for i in [ 1..3 ] do
yield ProvidedParameter("Param" + string i, typeof<string>) ],
IsStaticMethod = true, returnType = typeof<string>,
InvokeCode = (fun args ->
<@@
let dim1 : string = %%args.[0] : string
let dim2 : string = %%args.[1] : string
let dim3 : string = %%args.[2] : string
// let dims = [for %%arg in args do yield (arg : string) ]// [1] error below
// let dims = [for arg in args do yield (%%arg : string) ]// [2] error below
let dims = [ dim1; dim2; dim3 ] //this works
String.Join("--", dims)
@@>))
我想在一个列表中收集所有参数。
我尝试过但没有奏效的内容在代码引用中进行了注释。
[1]: [FS0010] Unexpected prefix operator in expression
[FS0594] Identifier expected
[2]: [FS0446] The variable 'arg' is bound in a quotation but is used as part of a spliced expression. This is not permitted since it may escape its scope.
按照以下方式破解您的解决方案实际上可以编译
InvokeCode = (fun args ->
let dims: string[] = Array.zeroCreate args.Length
let mutable i = 0
let inc () = i <- i + 1
<@@
while i < args.Length do
dims.[i] <- %%args.[i]
inc ()
String.Join("--", dims)
@@>
但我怀疑您更想将形状 [|Value ("a"); Value ("b"); Value ("c")|]
的 Quotations.Expr[]
转换为单个 Quotations.Expr
.
您可以使用 Microsoft.FSharp.Quotations.Patterns
中的模式按以下方式从表达式中提取内容
InvokeCode = (fun args ->
let dims =
args
|> Array.choose (function | Value(value, _) -> value |> string |> Some | _ -> None)
|> fun arr -> String.Join("--", arr)
<@@ dims @@>
这种解决方案也基于评论中建议的答案起作用:
ProvidedMethod(methodName = "GetContext",
parameters = [ for i in [ 1..3 ] do
yield ProvidedParameter("Param" + string i, typeof<string>) ],
IsStaticMethod = true, returnType = typeof<string>,
InvokeCode = (fun args ->
let dims = List.fold ( fun state e -> <@@ (%%string)::%%state @@>) <@@ []:List<string> @@> args
<@@
String.Join("--", dims)
@@>))