Unable to cast C# object in F# type

Viewed 78

I have the following declaration in F#:

type ConstArg =
    | Bool of bool
    | CharArray of char[]

member Lambda (ConstArg : ConstArg[]) = ...

In C#, I have constructed a char[] array of two values:

char[][] list = new char[][] {firstArray, secondArray};

I do want to call .Lambda(list) but I am unable to convert list into ConstArg[]

1 Answers

F# discriminated union type in C# is a class with static creational methods NewXXX for discriminated union cases. In your case there will be method with signature

public static ConstArg NewCharArray(char[] item)

You should use this method to convert each char array to an instance of ConstArg type:

Lambda(list.Select(ConstArg.NewCharArray).ToArray())

// or without LINQ
Lambda(Array.ConvertAll(list, ConstArg.NewCharArray))

Note that parameter names in F# should be camel-case as per naming conventions

Related