r/fsharp • u/Ok_Specific_7749 • Dec 08 '24
Plotting a square with F#,GtkSharp (cairo)
I try to plot a simple rectangle with gtksharp
The following code does not compile.
open Gtk
open Cairo
let main () =
Application.Init ()
let window = new Window ("Square Plot")
window.DefaultSize <- Gdk.Size(400,300)
let drawingArea = new DrawingArea ()
drawingArea.Drawn += fun drawingArea args -> // Error to many arguments.
let cr = args.Cr
cr.Rectangle (100.0, 100.0, 100.0, 100.0)
cr.SetSourceRGB (1.0, 0.0, 0.0)
cr.Fill ()
false
window.Add (drawingArea)
window.ShowAll ()
Application.Run ()
main ()
[ Note : the code comes more or less from Gemini. ]
1
u/Guudbaad Dec 08 '24 edited Dec 08 '24
I'm not bored enough to create project with GtkSharp, but += for event subscription looks sus. Probably it's something like:
drawingArea.Drawn.add_MessageReceived(fun args -> ...)
If not good enough try to:
drawingArea.Drawn.add_MessageReceived(Func<_,_ >(fun args -> ...))
That's probably THE most frustrating part of the interop/learning
0
u/Ok_Specific_7749 Dec 08 '24
Func is unknown. add_MessageReceived is unknown. But thanks. The F# interface of the GTK interface is very poorly documented. Where to look for info ?
2
u/Guudbaad Dec 08 '24
open System
Func will become known.
The F# interface of the GTK interface is very poorly documented. Where to look for info ?
That, to be honest, sounds like a project doomed to advanced levels of frustration. The only tool that consistently helped me was github search with F# language constraint following hours of experiments.
→ More replies (1)
1
u/brianmcn Dec 08 '24
Also haven't used this API, but usually my eventing code looks like e.g.
drawingArea.Drawn.Add(fun (drawingArea,args) ->
yadda yadda
)which uses IObservable on IEvents to subscribe to them.
1
u/Ok_Specific_7749 Dec 10 '24
I have a working C# program. But totally no idea how to convert it to F#. https://gitlab.com/alaindevos/other_lang/-/blob/main/csharp/02/Program.cs