GNU bug report logs -
#31154
predicate function for foreign-object not obvious
Previous Next
Full log
View this message in rfc822 format
The reference manual (guile-2.2.3) does not provide instruction on how to generate
a predicate for a foreign object, and it is not obvious. Maybe it doesn't need to
be in the manual but should then be in an example somewhere, IMO.
The following patch to the reference manual is one option:
--- doc/ref/api-foreign-objects.texi-orig 2018-04-14 11:26:35.779502997 -0700
+++ doc/ref/api-foreign-objects.texi 2018-04-14 11:28:42.823072217 -0700
@@ -96,6 +96,33 @@
needed.
@end deftypefn
+To generate a predicate for a foreign object use the @code{SCM_IS_A_P}
+macro as follows.
+
+@example
+static SCM foo_type;
+
+static SCM
+scm_foo_p(SCM obj) {
+ return SCM_IS_A_P(obj, foo_type)? SCM_BOOL_T: SCM_BOOL_F;
+}
+
+void
+init_foo()
+{
+ SCM name, slots;
+
+ name = scm_from_utf8_symbol("foo");
+ slots = scm_list_2 (scm_from_utf8_symbol ("data"),
+ scm_from_utf8_symbol ("deps"));
+ finalizer = NULL;
+ foo_type = scm_make_foreign_object_type(name, slots, finalizer);
+
+ scm_c_define_gsubr("foo?", 1, 0, 0, scm_foo_p);
+ ...
+}
+@end example
+
One can also access foreign objects from Scheme. @xref{Foreign Objects
and Scheme}, for some examples.
This bug report was last modified 7 years and 61 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.