Re: any policy for foo_new() return types?



On 22 Nov 2000, Owen Taylor wrote:

> 
> Mark Leisher <mleisher crl nmsu edu> writes:
> 
>>     Stefan> I believe that it's  common policy to return a GtkWidget* if
>>     this Stefan> is  available in the ancestry, and  otherwise fall back
>>     to a Stefan>  GtkObject -- but why this policy,  anyway?  And why do
>>     some of the Stefan> above examples draw away from this policy?
>> 
>> I really like GTK (but don't use GNOME).  After many years of using
>> the Intrinsics, I got used to object creation calls all returning
>> Widget*, and all widget-related functions taking Widget* arguments,
>> leaving the functions to check for valid subclasses instead of the
>> programmer doing it explicitly.
> 
> ALthough I haven't talked to Peter about the issue, so I can't
> tell you the _real_ reason for it, my take on the reasons why
> having functions take the most derived type useful is:
> 
> - It makes the header files clearer 
> 
> - Combined with standard programmer habits, it provides some measure
>   of compile time safety; because if I write:
> 
>    GtkWidget *box = gtk_hbox_new (FALSE, 0);
>    GtkWidget *entry = gtk_entry_new ();
>    gtk_container_add (GTK_ENTRY (entry), GTK_CONTAINER (box));
> 
>   The compiler will catch it. Of course, if I write  
>   
>    GtkWidget *box = gtk_hbox_new (FALSE, 0);
>    GtkWidget *entry = gtk_entry_new ();
>    gtk_container_add (GTK_CONTAINER (entry), GTK_ENTRY (box));
> 
>   It won't get caught until run time, but that's a harder mistake
>   to make.

Now if it returned its most derived type, the compiler would always catch
it.  Plus it'd prevent the confusing practice (that I've been guilty of
sometimes) of reusing a variable for different kinds of widgets.
As for normally immediately using the widget in container_add (or
widget_show), I think that's not worth it for the lack of static typing
(which I have come to like over the years).  In general, I think the return
types (and call types) should be the most specific possible without
sacrificing generality.

-Lars

-- 
Lars Clausen (http://shasta.cs.uiuc.edu/~lrclause) | Hårdgrim of Numenor
"I do not agree with a word that you say, but I    | Retainer of Sir Kegg
will defend to the death your right to say it."    |   of Westfield
    --Evelyn Beatrice Hall paraphrasing Voltaire   | Chaos Berserker of Khorne




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