[Top][All Lists]

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

Applicable structs and vtables

From: Zelphir Kaltstahl
Subject: Applicable structs and vtables
Date: Fri, 13 Aug 2021 20:33:33 +0000

Hi Guile users,

I have some code, in which I wrap procedures in a record, which contains
members, which express the arity of the procedures. For example one struct be:

<arity-function: min: 1, max: 3, function: (some lambda here)>

Something like that.

A while ago someone mentioned applicable structs and now I am curious, whether
perhaps Guile itself already provides something, that I can use to store arity
information in, while having something that is simply applicable by being the
first thing in an S-expression, so that it all looks and works like normal
procedure application.

The downside to my current approach of defining a normal record is, that I
cannot use (apply ...) on it. I have to use a special custom
(arity-function-apply ...) to access and actually apply the function stored in
the record. And this is infectious. All code, which wants to make use of any
<arity-function>, needs to use (arity-function-apply ...) or whatever its name
is, unless I access the record's member directly, which would also be bad,
because of overstepping abstraction barriers.

I could go an alternative way of storing information about the functions in some
global state, some hash-table probably, but I actually wanted to avoid using
global state. The book I am reading does it that way already, but I would like
to try to get the same functionality without using global state. (My code is

The upside of being able to use applicable structs, I imagine, would be, that I
could store arity information in them and and then use them like normal
procedures, using (apply …) or simply (my-arity-func-here …).

So I looked it up and found:

1. my bookmark of the previously mentioned code, which makes apparently use of
applicable structs:
which led me to

which led me to


But I have no idea what the pwpwpw stuff is all about or what p and w stand for
or why they are important. I have a hunch, that they somehow give information
about how to arrange stuff in memory, so that the compiler knows what size the
struct is, or something similar. Perhaps I am completely wrong though.

There is also the question, why such a struct is applicable. What makes it 

Is my idea of using such a struct actually a valid idea? The docs say:

> A vtable is itself a structure. It has a specific set of fields describing
various aspects of its instances: the structures created from a vtable. Some of
the fields are internal to Guile, some of them are part of the public interface,
and there may be additional fields added on by the user.

"additional fields added on by the user" sounds exactly like what I am looking 

I also found:
which has an example:

scheme@(guile-user)> (define <mything-vtable> (make-struct 
<applicable-struct-vtable> 0 'pw))
scheme@(guile-user)> (make-struct <mything-vtable> 0)
$1 = #<struct:23ce1e0 pw 24a8ec0>
scheme@(guile-user)> (make-struct <mything-vtable> 0 (lambda () 10))
$2 = #<struct:23ce1e0 pw 24b7920 proc: #<procedure 24b7940 at <current 
input>:4:32 ()>>
scheme@(guile-user)> ($2)
$3 = 10 

Looks like it really produces something applicable and I could stuff my actual
function in that struct and somehow add additional information about arity.

If this is a valid approach for having functions which have information about
their arity, then the next question is: What other downsides, if any, are there,
if I use these applicable structs?

Best regards,


reply via email to

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