This is the mail archive of the guile@cygnus.com mailing list for the Guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: New smob interface


Mikael Djurfeldt <mdj@mdj-pc.nada.kth.se> writes:

> Greg Badros <gjb@cs.washington.edu> writes:
> 
> > /* begin */
> > SCM
> > scm_make_my_type (int a, SCM b)
> > {
> >   SCM z;
> >   my_type_t *m = scm_must_malloc (sizeof (my_type_t), "my-type");
> >   m->a = a;
> >   m->b = b;
> >   SCM_NEWSMOB (z, my_type, m);
> >   return m;
> > }
> > /* end */
> > 
> > Shouldn't the "return m" be "return z"?
> 
> It should of course be z.  Sorry.

No problem-- glad to double check.

> > What do you folks think about a RETURN_SCM_NEWSMOB macro that would let
> > users write:
> > 
> > SCM
> > scm_make_my_type (int a, SCM b)
> > {
> >   my_type_t *m = scm_must_malloc (sizeof (my_type_t), "my-type");
> >   m->a = a;
> >   m->b = b;
> >   RETURN_SCM_NEWSMOB (my_type, m);
> > }
> 
> We guarantee that names in Guile start with SCM or scm (or gh).
> 
> The name should be SCM_RETURN_SMOB, SCM_RETURN_NEW_SMOB or something
> like that.  (The name NEWSMOB is chosen because of the similarity in
> use to NEWCELL.)

Ok.

> I won't kill you if you add this.  Maybe other people have strong
> opinions in this case?

Glad to hear I can keep my life! :-)

> 
> > #define RETURN_SCM_NEWSMOB(t,m) do { SCM __guile_smob_answer; \
> >                                      SCM_NEWSMOB(__guile_smob_answer,(t),(m));\
> >                                      return __guile_smob_answer; } while (0)
> 
> What is the advantage of using do ... while compared to just using
> braces?

Well, you're trying to have a macro act like a statement;  using the do
... while(0) idiom makes it really be a statement.  Suppose one writes:

if (...)
  SCM_RETURN_NEWSMOB(...);
else
  ... do something else ...

With a definition of SCM_RETURN_NEWSMOB that doesn't use the do...while
idiom, you get an expansion like:

if (...)
  {
   ...
  }
;  /* semicolon is from the programmer's text (not the expansion) -- he's treating
      SCM_RETURN_NEWSMOB as a statement */
else  /* this else is thus invalid */
  ... do something else ...

This is no good.  See the macro pitfalls section of the cpp info
page. For lots of details, see my paper "An Empirical Study of C
Preprocessor Usage" (this pointer is to a slightly old version-- if
anyone is interested I can get the newer version up and point you at
that):

http://www.cs.washington.edu/homes/gjb/papers/empirical-tr970406-abstract.html

There are a *lot* of macros in guile that are "latent bugs" in this
respect.  It's just good common practice to wrap compound statement
macros in do while (kinda like putting uses of macro arguments in
parentheses -- it might not bite you, but it makes using the macro
fragile if you don't).

Greg

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]