Re: Why do #coerce _and_ #as_widget exist?

Adrien Nader <[email protected]> Sun, 15 Sep 2013 11:55:50 +0200
Newsgroups gmane.comp.lang.ocaml.lib.gtk
Message-ID <[email protected]>
On Sun, Sep 15, 2013, Jacques Garrigue wrote:
> On 2013/09/14, at 23:55, Adrien Nader <[email protected]> wrote:
> 
> > I'm working on a presentation about lablgtk and there's something I've
> > always had issues with: #coerce and #as_widget.
> > 
> > The README file says:
> > * a coerce method, returning the object coerced to the type widget.
> > * an as_widget method, returning the raw Gtk widget used for packing,
> >  etc...
> > 
> > However, for the #pack method of boxes for instance, it's #coerce which
> > has to be used, not #as_widget.
> > 
> > Was it impossible to hide these conversions away from the user?
> > 
> > Why are there two methods which do almost the same thing? Was it not
> > possible to only require the user to call #coerce and then do #as_widget
> > automatically inside the bindings?
> 
> In the high-level interface, you never need to call #as_widget.
> As you say, this is automatically done by the bindings.
> However, we still need a method #as_widget, because the bindings
> themselves need a way to access the raw widget, and you access fields
> directly, or define private methods in ocaml.
> Well, actually since the introduction of private row types, it is kind of possible,
> but still not very flexible (i.e. we could make widget_misc a private row type,
> and hide low-level methods in it, as it is never extended).
> 
> > What are the criteria to know when to use which?
> 
> #as_widget should only be used inside bindings, or when you want to
> use the low-level interface.
> 

Thanks, that's clear.

I've noticed a few calls to #as_widget in the examples:
> % git grep '#as_widget' | grep -v inherit
> assistant.ml:  ignore (assistant#append_page box#as_widget);
> assistant.ml:  assistant#set_page_complete box#as_widget true;
> assistant.ml:  assistant#set_page_type box#as_widget `SUMMARY;
> assistant_tutorial.ml:  assistant#set_page_complete toggle#as_widget active
> assistant_tutorial.ml:    page_0#as_widget;
> assistant_tutorial.ml:    page_1#as_widget;
> assistant_tutorial.ml:    page_2#as_widget;
> assistant_tutorial.ml:    page_3#as_widget;
> assistant_tutorial.ml:    page_4#as_widget;
> buttons.ml:  new GObj.widget_full box#as_widget
> sourceview/sourceview2.ml:    Some (label#coerce#as_widget)

I'm not familiar with the assistant and sourceview(2). Are they mistakes
or on purpose?

-- 
Adrien Nader