How to use a C# library from F#?

Hello,

I am new to F# (and .Net). Never programmed in C# or on dotnet. Just started learning F# about a month or so back to explore using it, since it can be now used on Mac and Linux.

I was trying to see if I can use a C# library, Cocona from F#.
For now I am wondering how to translate the following code example from the Github page of the library to F#.

var builder = CoconaApp.CreateBuilder();
var app = builder.Build();

app.AddCommand((string name) => { 
     Console.WriteLine($"Hello {name}");
});

app.Run();

I created an F# project and added CoconaLite to the project using dotnet add command. Then I typed the following:

let builder = CoconaLiteApp.CreateBuilder();
let app = builder.Build();

app.AddCommand (fun (name: string) -> printfn "My name is {name}." |> ignore)

app.Run()

If I try to do this, I get he following error:

error FS0041: No overloads match for method 'AddCommand'.Known type of argument:
(string -> unit)Available overloads: - (extension)
Builder.ICoconaCommandsBuilder.AddCommand(commandBody: Delegate) :
Builder.CommandConventionBuilder // Argument 'commandBody' doesn't match -
(extension) Builder.ICoconaCommandsBuilder.AddCommand(commandData:
Builder.ICommandData) : Builder.ICoconaCommandsBuilder // Argument 'commandData'
doesn't match
[/Users/curioustolearn/LearnSoftware_and_Math/LearnFsharp/App1/App1.fsproj]

The build failed. Fix the build errors and run again.
The build failed. Fix the build errors and run again.

Can someone please let me know if it is possible to use a C# library like Cocona from F#, and a simple example of how to achieve what I am trying to do above. Any link to a resource or example that helps with this question would be helpful too.

Thank you very much.

Hello! F# language with strong explicit cast types.
And in your case you only need provide true type for c# method (Delegate)


let cmd = System.Action<_>(fun (name: string) -> printfn "My name is {name}.")

app.AddCommand cmd |> ignore

Thank you so much @altbodhi. This worked great. Can you please point me to some resources where I can learn more about this? How did you know to use System.Action, or what the type for the C# method is? I don’t know C#. Do I need to learn that for this kind of stuff? Does any book or online docs on F# cover this?

Thank you again.