@c -*-texinfo-*-
@c This is part of the GNU G-Golf Reference Manual.
@c Copyright (C) 2020 - 2021 Free Software Foundation, Inc.
@c See the file g-golf.texi for copying conditions.


@node Selective Import
@subsection Selective Import

To selectively import namespace components, use @ref{gi-import-by-name},
which takes two arguments, a @var{namespace} and a (component)
@var{name}. Let's try on our minimal @samp{Hello World!} example and see
how it goes. All we need to do, is to substitute the @code{(gi-import
"Gtk")} call by the following expression:

@lisp
(for-each (lambda (name)
            (gi-import-by-name "Gtk" name))
    '("Application"
      "ApplicationWindow"
      "Button"))
@end lisp

With this change, everything else kept equal, if you (quit and) restart
Guile, evaluate the updated @samp{Hello World!} example code, you will
notice how the elapse time before the application window appears is now
substantially reduced, compared to the version that imports the all
@code{Gtk} namespace. Substantially reduced but @dots{} not
instantaneous: well, that is expected!

Although we only import a few @code{Gtk} namespace components, three
GObject classes in this example, G-Golf will import those classes, their
interface(s) if any, methods, enums, flags ... and do the same for their
parent class, recursively. For those three classes only, G-Golf actually
has to import (and dynamically define) tens of classes, interfaces,
enums, flags @dots{} as well as hundreds of methods and procedures.

@c The time G-Golf takes to get ready to launch your application or its
@c first dialog is directly proportional to the number of elements and
@c dependencies it has to import. You may check for yourself, how fast
@c things are, if you import an element that has no dependencies (or nealry
@c no dependencies

G-Golf will also import classes, interfaces and their dependencies
(enums, flags @dots{} recursively as well @dots{}) from other namespace
if necessary. We already have an illustration of this, both with the
original example and the change we just made: although we do not
explicitly import the @code{GApplication} class from the @code{Gio}
namespace, G-Golf did that for us, and so we may call @code{run} - which
is the short method name for @code{g-application-run} - as if we did
manually import it.

Both the @var{namespace} and @var{name} arguments are case
sensitive. The @var{name} argument is used to retrieve the typelib
@ref{Base Info} that holds the metadata of the introspectable library
element it represents. Although there are a some exceptions, it is
generally derived from and obtained by dropping the @var{namespace}
prefix (without its version number if any) out of the original
name. Here are a few more examples, organized by @var{namespace}:

@indentedblock
@table @code

@item Gtk
GtkWindow -> Window	@*
gtk_init -> init	@*
gtk_main -> main	@*
gtk_main_quit -> main_quit @*
@dots{}

@item WebKit2
WebKitWebView -> WebView	@*
WebKitLoadEvent -> LoadEvent	@*
@dots{}

@item @dots{}
@end table
@end indentedblock


@ @ @sup{_} @strong{Cool, selective import, but what about scripting?}

Right! The 'Hello World!' example we have presented so far can only be
run interactively.

In the next section, we will see how we may turn it - and any other
example or application - so it can be run as a script.

@c (@pxref{Running Guile Interactively,,, guile, The GNU Guile Reference
@c Manual})
