虽然将部分函数传递到另一个函数并不重要,但如何从函数返回不同签名的部分函数?f#从函数返回部分函数
这里是我尝试的基本代码,再进行各种尝试,以得到它的工作:
type InitData() =
static member arrayIntAsc count = [|1..count|]
static member seqIntAsc count = {1..count}
static member listIntAsc count = [1..count]
(*more diverse signatures*)
module x =
let getInitDataFun (initData:string) =
match initData.ToLower() with
| "arrayintasc" -> InitData.arrayIntAsc
| "seqintasc" -> InitData.seqIntAsc
| "listintasc" -> InitData.listIntAsc
(*more diverse signatures*)
| _ -> failwithf "InitData function %s not recognized" initData
试图迫使以各种方式通用的返回签名,但F#3.0总是迫使getInitDataFun返回签名的第一个匹配的签名:
let getInitDataFun (initData:string) : 'a -> 'b = ... let getInitDataFun (initData:string) : _ -> _ = ... let getInitDataFun (initData:string) : int -> #(int seq) = ... let getInitDataFun (initData:string) : int -> #('a seq) = ... (*even if I could get (int -> #(int seq)) to work, I would like to return signatures not in this pattern too*)
试图盒/拆箱:
| "arrayintasc" -> box InitData.arrayIntAsc
这个编译,但拆箱尝试抛出一个运行时错误:
Unhandled Exception: System.InvalidCastException: Unable to cast object of type
'[email protected]'
to type'System.Collections.Generic.IEnumerable`1[System.Object]'
试图恢复部分功能的报价,但有类似的问题。如果我返回键入的引用,则返回不同的Expr签名也会遇到同样的问题。我可以返回无类型的引号,但是我必须知道在调用端返回的无类型表达式的签名。
被认为是反射,但基本上是相同的问题,需要知道实际的签名时,它需要调用。
也以各种方式尝试了上传部分功能。
你能解释一下为什么你要这样做吗?你如何期待调用者使用'getInitDataFun'? – kvb