Gtk Signal_connect and piping

Hi, I decided to try piping instead of do-block but wo success

using Gtk
b = GtkButton(“Press me”)
win = GtkWindow(b, “Callbacks”)
id = signal_connect(b, “clicked”)|>w->println(w, " was clicked!")
showall(win)

ERROR: MethodError: no method matching signal_connect(::GtkButtonLeaf, ::String)
what I did wrong?Thanks in advance

This is due to how pipes work: they call the function on the right with the value on the left, which is not what you want here: doing

signal_connect(b, "clicked") do w
    println(w, " was clicked")
end

is an alternative syntax for

signal_connect(w->println(w, " was clicked"), b, "clicked") 

In both cases the callback is passed as first argument, as required by signal_connect.

On the other hand, the following

signal_connect(b, "clicked") |> w->println(w, " was clicked!")

tries to execute signal_connect(b, "clicked") and pass the result to the function w->println(w, " was clicked!"). Another way to look at it: the following uses the pipe syntax correctly:

"hello" |> w->println(w, " was clicked!")

Obviously you cannot replace “hello” with the signal_connect call…

I knew both forms you gave but I didn`t understand that anonim function after do statement become a part of signal_connect function like in doc example with map function.

map([A, B, C]) do x
if x < 0 && iseven(x)
return 0
elseif x == 0
return 1
else
return x
end
end

I decided that with do-block map function send A,B,C to anonim function but it works as usual, applying anonim function to A,B,C.
Many thanks.

1 Like