StefanSalewski / gintro

High level GObject-Introspection based GTK3/GTK4 bindings for Nim language
MIT License
296 stars 20 forks source link

Allow to use lambdas in signals #90

Open gavr123456789 opened 4 years ago

gavr123456789 commented 4 years ago

I'm trying to put a lambda here instead of buttonClicked button.connect("clicked", (btn) => btn.label = utf8Strreverse(button.label, -1)) But got error: Error: expected: ')', but got: 'buttonClicked'

StefanSalewski commented 4 years ago

Use of lambdas has never been advertised in the README, in the GTK 4 book or elsewhere.

I think I had a discussion with Araq or maybe another core dev about 4 years ago about the fact that lambdas do not work with the current shape of the connects macro, but I can not remember details. Araq and the other core devs are generally very busy and not too much interested in language bindings that are uses only by a few people, and I think they like GTK not that much at all. And Araq told me a lot of times that he would like c2nim style bindings with a single pre-generated file with a lot of when statements for various OS and 32/64 better. There is a section in the GTK4 book about this topic, I do not want to repeat the arguments again and again.

If you are really good at writing Nim macros or if you are willing to hire someone who is really good in it then we may consider replacing our current macros system with another one. One additional improvement would be that the optional last parameter in connec() which in C is a void pointer can currently not be a Nim var parameter. Var parameters are rarely used, but when you work on it, you may consider that as well.

I think Araq would love macros that work only on the AST and does no string manipulation, so we should keep that in mind. And Araq would better like macros with no string parameter, so not button.connect("click", myHandler) but something like button.connect_click(myHandler). I think some bindings for other languages do that shape, it may be Rust or Go. Ruby at least used the string, C also of course. Maybe you should ask in IRC or the forum which form people prefer. I used the string form because that was familiar for most GTK users that time.

Please make sure that all the existing examples work fine with your new macros and request a code review from some of the Nim core devs before making a pull request.

gavr123456789 commented 4 years ago

If you are really good at writing Nim macros

Looks like I should explain my story better. I have been developing on Vala for a long time, my main job now is to develop an ERP system in C# GTK.

In both of these languages signals are members of the first class:
Vala:

public class Test : GLib.Object {

    public signal void sig_1(int a);

    static void main(string[] args) {
        Test t1 = new Test();

        t1.sig_1.connect((t, a) => {
            stdout.printf("%d\n", a);
        });
        t1.sig_1(5);
    }
}

And they are binded as events for C#. This has several advantages, for example, you can see signals in autocomplete and distinguish them from other functions.
image
image

It seems to me that signals are text in scripting languages, perhaps because they communicate with GTK via GIR in runtime.

So, I want to start a new pet project and I was choosing between kotlin and nim, for quite a long time, and I chose nim because of the GTK bindings (I didn't like tornado/javafx) and the procedural paradigm, I'm really tired of enterprise OOP xd.

I started learning more about nim just 2 days ago, so I won't be able to create new macros for GTK)

After a brief review of the examples, it seems to me that the biggest problem is the tables with GValue. Here how they looks on Vala. This is also not very good, but probably the best that you can get with GTK if you do not make your own builder pattern(1 2 ).

After all I think that using metaprogramming in nim you can make the best bindings possible!, https://github.com/glurp/nimgtkdsl

gavr123456789 commented 4 years ago

PS I think it's better to focus on the GTK examples from Vale than on C, since this is a higher-level language that was created for the purpose of writing GTK applications.
The first 4 links here contain many examples.

And this man got the examples for GTK 4. https://github.com/aeldemery

StefanSalewski commented 4 years ago

my main job now is to develop an ERP system in C# GTK.

I think you told me about your ERP project in the Nim forum already.

I have to be a bit carefully to not make the Nim devs angry, but do you really need Nim for this? I think you said that you know Kotlin well, and Kotlin is also an interesting language. You should not need the high performance of Nim for your project.

And for GTK: Gtk has unfortunately no mobile support, and no real support for web apps. Well there is libhandy and broadway, but I am not sure if that really helps.

And for GTK bindings: I think Python and Rust bindings are not in a bad shape, JavaScript bindings may be OK, but I know that some people had trouble with it. Vala bindings should be perfect of cause, as Vala is a pure GTK project. But Vala is death.

Creating perfect bindings is unfortunately a very big effort. Well maybe small still compared to creating Qt bindings. But the effort is large. Rust, Python, GTKMM, all these projects have spend a very large effort in bindings, and still number of users is very small.

I spent about 1500 hours on gintro, and before 600 hours on oldgtk3. Fully unpaid of course. As I have to pay some bills, I should try really to spent not that much time for it. There are open issues of course, you mentioned GValue type. And there are GLists, some Arrays, lambda syntax. So at least work for a few hundred more hours. And then we have to do testing. GTK related modules have at least 10000 functions, and 2000 data types. We have tested maybe one percent of it, so there may be bugs and more work. From Nim core devs we get no help, they like GTK not much. And as there are so few users we can not expect that core devs really spent time for gintro and GTK.

When your project is commercially, you may consider hiring a bright core dev for a few weeks, maybe for lambda syntax support. Maybe you would have to hire a GTK expert also, there is at least ebassi. I am generally willing to add work from bright core devs to gintro. And of course you are free to fork it.

As you are still new to Nim: Maybe when you really want to use GTK and you want to use a statically compiled language, then you may consider using Rust. Rust has a much larger community, with a few GTK devs lead by Mr Droege.

And when you really want to use Nim: I have listed most of the other Nim GUI toolkits in the GTK4 book. Maybe you could use one of them. Of course GTK is much more complete, but maybe using the other toolkits is more fun, and maybe it is easier to get unpaid core dev support for them. I think Araq like fidget one.