Next: , Previous: , Up: Reference Guide   [Contents][Index]


3.5 Utilities

The following procedures are in the (gi util) library.

3.5.1 Utilities for Guile Modules

In core Guile, there are options to tailor handling of same-named procedures imported into the same module (see Creating Guile Modules in Guile Reference Manual). The default-duplicate-binding-handler procedure and the #:duplicates option to define-module can be used to tailor the strategy. Choosing a correct behaviour may become important important when using GObject introspection when many methods, procedures, and signals may have the same name.

In the (gi util) library, the following additional helper procedures are provided.

Procedure: push-duplicate-handler! handler

This utility procedure adds the symbol handler to the list of default duplicate handlers. handler will be placed at the start of the list, giving it highest priority.

Duplicate handler: shrug-equals

In the case of duplicate bindings whose values are equal (as in eq?), they are accepted without raising a warning.

Another strategy to mitigate the problems posed by same-named methods, procedures, and variables is to use the #:renamer option of use-modules or use-typelibs. Core Guile provides the symbol-prefix-proc renamer procedure, which can be used to apply a prefix to the name of all imported procedures and variables.

This module provides the protect* renamer. It is more targeted than symbol-prefix-proc; it applies a prefix and/or suffix to those imports whose names are in a list of provided symbols.

Procedure: protect symbol [prefix] [suffix]
Procedure: protect* symbols [prefix] [suffix]

Returns a renamer, which “protects” symbol, a symbol or symbols, a list of symbols from being overridden, by prepending prefix and appending suffix, both symbols. If neither prefix nor suffix are given, '% is used as prefix.

Variable: %rnrs-syntax
Variable: %r5rs-procedures

Constants, which can be applied to protect*.

%rnrs-syntax contains R6RS syntax keywords from the (rnrs syntax) module. Most of these are also very important to R5RS.

%r5rs-procedures contains the names of common R5RS procedures that Guile always provides.

These can be used alone or together to protect important names in Scheme code. The following example, for instance, prepends gtk:: to imported GObject procedures which match R6RS syntax or R5RS procedures.

(use-modules (gi)
             (gi util))
(use-typelibs (("Gtk" "3.0")
               #:renamer (protect*
                           (append %rnrs-syntax %safe-r5rs)
                           'gtk::)))

While importing procedures or methods with the same names a Scheme syntax may cause confusion, importing procedures or methods with the same name as Guile procedures is usually not a problem due to method overloading via GOOPS.

Note: While this can prevent you from accidentally messing up important procedures, it is usually a better strategy to only include what you need.

3.5.2 Utilities for Vectors

In the (gi util) library, the following helper procedures are provided for use with SRFI-4-type uniform numeric vectors.

Procedure: short-vector->list vec
Procedure: int-vector->list vec
Procedure: long-vector->list vec

Converts a SRFI-4 uniform numeric vector of signed integers into an integer list.

Procedure: list->short-vector lst
Procedure: list->int-vector lst
Procedure: list->long-vector lst

Converts a list of integers into an SRFI-4 uniform numeric vector of signed integers.


Next: Compatibility, Previous: Enums and Flags, Up: Reference Guide   [Contents][Index]