[gtkada] Gtk.Containers and derived types

Dmitry A. Kazakov mailbox at dmitry-kazakov.de
Wed Aug 24 09:39:05 CEST 2011


On Tue, 23 Aug 2011 21:05:03 +0100, you wrote:

> I'm trying to use the procedure Gtk.Container.Remove on a hbox container 
> and I get a compiler error because it is expecting a Gtk.Container 
> access type rather than the Hbox I try to use in the parameter.

OK, it is frequently and wrongly said that fully qualified names are good.
They are not as you have discovered.

Your problem is that when you call Gtk.Container.Remove it means what it
tells, an operation defined on Gtk_Container_Record. This is *not* the type
of a box, which is Gtk_Box_Record. Ada is a typed language!

Remove is a primitive operation inherited from Gtk_Container_Record by
Gtk_Box_Record. The result of inheriting is an operation of its own. So if
you want to name it, then the name is Gtk.Box.Remove. So:

   Gtk.Box.Remove (Box, Child);

(You could also convert Gtk_Box_Record to Gtk_Container_Record and call the
parent's operation, which is extremely dangerous and is actually the reason
why the compiler fagged Gtk.Container.Remove (Box, ...) as a type error.
What if Remove was overridden?)

Moral: Use "use"! Designs based on fully qualified names are fragile and
require full understanding of how and where operations are defined.

Without fully qualified names:

In Ada 95:

   with Gtk.Box;  use Gtk.Box;
   ...
   Remove (Box, Child);

In Ada 2005 you can also use the prefix notation:

   Box.Remove (Child); -- No need in "use Gtk.Box"

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



More information about the gtkada mailing list