Janthelme
Janthelme

Reputation: 999

Polymorphism in Excel Dna / F#

In F# / Excel-Dna, what is the idiomatic way to rewrite the following function for a vector of strings? (i.e. a function which sorts a "vector" (=1d Excel range) of strings).

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

If I merely replace the double types with string types in the above snippet, I get this error message : Initialization [Error] Method not registered - unsupported signature, abstract or generic: 'MyFSFunctions.mySortString'

I saw this previous question where Govert suggests to use the "Registration extensions" but I have not found how to use it to answer my current question.

Upvotes: 3

Views: 141

Answers (2)

humhei
humhei

Reputation: 11

For Registration samples

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

Upvotes: 1

Jim Foye
Jim Foye

Reputation: 2036

As you have mySortDouble written, it won't even compile, because it returns a double[], not a double.

Here's an example that works, with some minimal error handling added.

[<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

Upvotes: 4

Related Questions