gircore / gir.core

A C# binding generator for GObject based libraries providing a C# friendly API surface
https://gircore.github.io/
MIT License
312 stars 29 forks source link

F# examples #353

Open gavr123456789 opened 3 years ago

gavr123456789 commented 3 years ago

Provide F# code examples

badcel commented 3 years ago

Thank you for your suggestion.

To test if the current code works well for F#, too some initial samples would be great.

Perhaps we need to provide some additional F# layer to make the library convenient to use from F#.

If you are interested in creating some exemplary samples feel free to create a PR.

I think optimization for F# won't be on the roadmap for the first release, except someone from the community steps up. But in the long run I think F# should be equally important to us as C#.

gavr123456789 commented 3 years ago

If you are interested in creating some exemplary samples feel free to create a PR

I have just started learning F#, but I have already fallen in love with the ML family. Yesterday I came across this question, and only now it occurred to me that in addition to haskell from functional languages for programming on GTK, F# will be available soon, and it seems to me that it is better suited for GUI since it defines OOP and FP, unlike purely functional Haskell.

gavr123456789 commented 3 years ago

I just tried to make a Window example in F#, and it worked. Now I'm super excited.

open Gtk;

[<EntryPoint>]
let main argv =
    Functions.Init();
    let mainWindow = new Window("Sas")
    mainWindow.ShowAll()
    Functions.Main();
    0

image So far, I'm not sure how to bind the signal, instead of onDestroy there is add_OnDestroy, although I think it works as well.

badcel commented 3 years ago

Thanks for testing F#. Great to see that it is working! :rocket:

I'm not sure how signals will work with F#. I would need to dig into it myself.

In general as C# and F# use the same IL you can currently either connect to the event (see here) or the property descriptor (see here) with the corresponding F# syntax.

jwosty commented 2 years ago

If its exposed as an event (speaking on this without having run any of this code yet), you should be able to just do this in F#:

// With no cleanup
someEvent.Add (fun e -> printfn "got the event")

// With cleanup
let subscription : IDisposable = someEvent.Subscribe (fun e -> printfn "got the event 2")
// then, later on (or could have been in a use binding)
subscription.Dispose ()

Events have the nice property of being able to be used in Observable pipelines.

Alternatively, if we're thinking idiomatic F# layers in here, it could be a MVU style api like Elmish / Fabulous. Though that can be certainly just be layered on top of a more CSharp-y design.