Excel Dna / F#中的多态性

问题描述 投票:3回答:2

在F#/ Excel-Dna中,为字符串向量重写以下函数的惯用方法是什么? (即对字符串的“向量”(= 1d Excel范围)进行排序的函数)。

[<ExcelFunction(Category="Some Cat", Description="Sort 1d range filled with doubles.")>]
    let mySortDouble (vect : double[]) : double[] = 
        Array.sort vect

如果我只是在上面的代码片段中用字符串类型替换double类型,我收到以下错误消息:Initialization [Error] Method not registered - unsupported signature, abstract or generic: 'MyFSFunctions.mySortString'

我看到this previous question Govert建议使用“注册扩展”,但我还没有找到如何使用它来回答我当前的问题。

f# excel-dna
2个回答
4
投票

由于你编写了mySortDouble,它甚至不会编译,因为它返回double [],而不是double。

这是一个有效的示例,添加了一些最小的错误处理。

[<ExcelFunction(Category="Some Cat", Description="Sort 1D range of strings.")>]
let SortStrings (vect : obj[]) = 
    try
        vect
        |> Seq.cast<string>
        |> Seq.sort
        |> Seq.toArray
        |> box
    with
    | ex -> box ExcelError.ExcelErrorNA

1
投票

For Registration samples

ParameterConversionConfiguration() .AddReturnConversion(fun (values: double[]) -> Array.map (string >> box) values )

© www.soinside.com 2019 - 2024. All rights reserved.