gir.core icon indicating copy to clipboard operation
gir.core copied to clipboard

F# examples

Open gavr123456789 opened this issue 4 years ago • 5 comments

Provide F# code examples

gavr123456789 avatar Jun 06 '21 23:06 gavr123456789

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#.

badcel avatar Jun 07 '21 19:06 badcel

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 avatar Jul 19 '21 23:07 gavr123456789

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.

gavr123456789 avatar Jul 26 '21 10:07 gavr123456789

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.

badcel avatar Jul 26 '21 13:07 badcel

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.

jwosty avatar Dec 16 '21 06:12 jwosty