Re: types checking signal/callback functions revisited




Steve Hosgood <iisteve@iiweeble.swan.ac.uk> writes:

> > 
> > I've been thinking about the problem of checking the types expected
> > by callback functions [...] but there are no compile- or run-time checks
> > that this is done correctly.
> > 
> > Now, the only way I can think of actually getting the compiler to
> > check this is at function declaration. So, if we defined a macro [....]
> > 
> > Similar things can be done with all the signal callbacks, and still 
> > allow us to keep the simplicity of using gtk_signal_connect.
> > 
> > Any thoughts or better ways of doing this?
> > 
> 
> I swapped a few ideas along these lines with Owen(was it?) and some of the
> other gurus a month or so ago. They seemed to think that there was a
> problem in that everyone (except me) likes to misdeclare the callback
> functions to something like:
> 
> 	void callback( GtkWidget *w, GtkWhatever *x )
> 	{
> 
> 
> ..rather than what *should* be done, i.e:
> 
> 	void callback( GtkWidget *w,  gpointer param )
> 	{
> 		GtkWhatever *x = param;

Well it certainly wasn't me. I know of no reasons why the
second is superior to the first, and use the first almost
exclusively. (For signals; in cases where we do have type
safety for function pointers, such as for gdk_input_add()
callbacks, I may use the second)

> Strict type-checking would stop this practice. I say "good!", others
> disagree. I've been blown up by some callback parameter mismatches
> in my time. I also note that most beginners like me waste a fair bit
> of list-bandwidth asking the same questions caused by such problems
> that they too are having.

Strict type safety is basically impossible without making the
entire signal system much less convenient. You'd basically
have:

 gtk_widget_connect_button_press_event (...)
 gtk_widget_connect_button_press_event_after (...)
 gtk_widget_connect_button_press_event_object (...)

etc, etc, etc, etc. And for minimal gain. Once you know
that event signals have a different prototype, it's not
a common mistake. The flexibility of the signal system
unfortunately makes it hard to type-check at runtime.
 
Regards,
                                        Owen



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]