Re: Figuring out enums and structs with foreigners

"felix.winkelmann--- via Chicken-users" <[email protected]>
Newsgroups gmane.lisp.scheme.chicken
Message-ID <[email protected]>
> I have a beginners question. Using `foreigners` one can easily define
> structs and enums. But how does one figure out whether a symbol is an
> enum or a struct?

This is not at all a beginners question. Normally you have no access rto
the internals of modules, but foreigners happens to define foreign types
so that the defined entities can be used in foreign function declarations.
The foreign type registry is part of the compiler and can be accessed
like this:

--
(import foreigners)

(define-foreign-enum-type
 (event unsigned-integer32)
 ...)

(define-foreign-record-type
 plan
 ...)

(define-syntax ftype
  (er-macro-transformer
    (lambda (x r c)
      ;; retrieve registered foreign type
      (print `(,(cadr x) ,(chicken.compiler.support#lookup-foreign-type (cadr x))))
      #t)))

(ftype event)
(ftype plan)
--

The value stored in the table is a 4-element vector holding the type
name, the "real" foreign type for which the user-defined type is an
alias and the names of two conversion procedures. I leave it to you
to experiment with this, it may be sufficient for your needs.

It goes without saying that this is a deeply immoral hack. It exposes
implementation details that may change at any moment (but are likely
to stay). Use at your own risk.


cheers,
felix
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.