NetCord icon indicating copy to clipboard operation
NetCord copied to clipboard

Provide extensions for F#

Open KubaZ2 opened this issue 11 months ago • 1 comments

Description

Here is how you would currently write a minimal API style bot in F#:

let builder = Host.CreateApplicationBuilder()

builder.Services
    .AddApplicationCommands()
    .AddDiscordGateway() |> ignore

let host = builder.Build()

host.AddSlashCommand("ping", "Ping!", Func<string>(fun() -> "Pong"))
    .UseGatewayEventHandlers() |> ignore

host.Run()

Currently, Func<string>(...) is needed for minimal API commands. To improve the experience, it would be needed to provide generic overloads with FSharpFunc<...>. This issue would also be resolved by https://github.com/fsharp/fslang-suggestions/issues/1131.

Of course extensions for services themselves would also be added.

I am also probably unaware of other places in which extensions for F# would be needed. Feel free to suggest them.

The package naming would be {PackageName}.FSharp, so for example it would be NetCord.Hosting.FSharp for NetCord.Hosting.

I think it would be better to write the extensions in F#, as only F# methods can be inlined and optimized by the compiler.

KubaZ2 avatar Jan 13 '25 14:01 KubaZ2

All things considered, I think there is no way to implement reasonable F# support for minimal APIs as of now (F# 9). F# emits wrappers for methods passed to delegates that do not contain attributes placed on the original method so it is basically impossible to get attributes of the method passed. That is why I think for now it's out of scope.

I will leave this issue open in case support for that gets implemented.

KubaZ2 avatar May 21 '25 21:05 KubaZ2