Re: Why do #coerce _and_ #as_widget exist?

Jacques Garrigue <[email protected]> Sun, 15 Sep 2013 08:49:37 +0900
Newsgroups gmane.comp.lang.ocaml.lib.gtk
Message-ID <[email protected]>
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.

	Jacques