[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Using (defined? foo) from C.

From: Marius Vollmer
Subject: Re: Using (defined? foo) from C.
Date: 29 Mar 2003 12:31:19 +0100
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3

Thamer Al-Harbash <address@hidden> writes:

> Is there anyway to create a symbol SCM in C without defining it
> in the current environment?

Yes.  Symbols are a data type of their own, independent of bindings,
environments and modules.  You can use

    scm_str2symbol ("foo")

in C to get the symbol with the name "foo".  Symbols in turn are used
to name things in environments and modules, but they can be used for
other things as well.

> If not is there a way of passing a C string as a symbol name to do
> reflection with? I'm looking for something as simple as evaluating
> (defined? foo) where foo is variable and receive the boolean value
> back.

Try this (with a big comment that Guile should provide it itself,

    static SCM
    false (void *unused1, SCM unused2, SCM unused3)
      return SCM_BOOL_F;

    static SCM
    wrapped_scm_c_lookup (void *data)
      return scm_c_lookup ((char *)data);

    defined_p (char *str)
      return !SCM_FALSEP (scm_internal_catch (SCM_BOOL_T,
                                              wrapped_scm_c_lookup, str,
                                              false, NULL));

The reason for this longwinded code is that scm_c_lookup either
returns a valid variable or throws an error.  We did this to have a
simple way to lookup variables and at the same time handle unbound
variables in a consistent manner.

Clearly, there should also be a function that return SCM_BOOL_F when a
name is unbound, but we don't have that yet.  It will appear when the
module system API as a whole, ahem, 'stabilizes'.

GPG: D5D4E405 - 2F9B BCCC 8527 692A 04E3  331E FAF8 226A D5D4 E405

reply via email to

[Prev in Thread] Current Thread [Next in Thread]