chicken-users
[Top][All Lists]
Advanced

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

Re: Question about how to check a symbol is bound


From: Thomas Chust
Subject: Re: Question about how to check a symbol is bound
Date: Fri, 23 Jun 2023 13:17:57 +0200

Hello,

if you are looking for record introspection capabilities, I would recommend looking into SRFI-99[1], which combines static structure definition and dynamic reflection APIs. You're example could look like this:

(import
(chicken format)
srfi-42
srfi-99)

(define-record-type egg-info
make-egg-info egg-info?
name author desc)

(define (print-record/fields r)
(define rtd (record-rtd r))
(do-ec (: f (rtd-field-names rtd))
(format #t "~a: ~a~%" f ((rtd-accessor rtd f) r))))

(print-record/fields
(make-egg-info
"F-operator"
"Kon Lovett"
"Shift/Reset Control Operators"))

Ciao,
Thomas

--
[1]: https://srfi.schemers.org/srfi-99/srfi-99.html


Am Fr., 23. Juni 2023 um 10:03 Uhr schrieb Pan Xie <xiepan@skyguard.com.cn>:
#+begin_quote
Perhaps if you can explain why you need to know if a symbol is bound or
unbound, we might be able to help you better achieve your goal.
#+end_quote


For example, if I want to do things shown in following codes, it is useful to get the
interned symbols from their names and also get their bound procedures:

  (define-record egg-info
    name author desc)

  (define (show-egg-info egg)
    (define (symbol-value sym)
      (##sys#slot sym 0))

    (define (getter field-name)
      (symbol-value
       (string->symbol
        (format #f "egg-info-~a"
                field-name))))

    (let ((fields '(name author desc)))
      (for-each
       (lambda (f)
         (format #t "~a: ~a~%"
                 f
                 ((getter f) egg)))
       fields)))

  (show-egg-info (make-egg-info
                  "F-operator"
                  "Kon Lovett"
                  "Shift/Reset Control Operators"))

I think it is a very common idiom in languages from Lisp family. So it is important to know
how to check symbol is bound and get its value. Every scheme implementation means for
business seriously should have the ability.

Thanks
Pan


On 6/23/23 15:40, Peter Bex wrote:
> On Fri, Jun 23, 2023 at 03:32:38PM +0800, Pan wrote:
>> Ah, that make sense. It seems I can just use the '##sys#slot' procedure to
>> accomplish all that tasks.
> Please don't use ##sys#slot unless you know what you're doing - the
> procedure is unsafe and will cause segmentation faults when used on
> non-block objects.  Anything that starts with ##sys# or ##core# etc
> is intentionally undocumented because it's not meant for user code.
>
>> Would you please elaborate about the "transformed
>> name"?  I see there are codes reference symbols like "##sys#slot" or
>> "scheme#list", but I can't find document describe them. Is there any
>> document I can look into? More specifically, how can I transfer "list" to
>> "scheme#list"? Is there procedure can do that?
> scheme#list is the fully qualified symbol that means it's the "list"
> procedure from the "scheme" module.  If you do "(import scheme)" and then
> just use "list" without prefix, it will get rewritten under the hood.
>
> Again, this is an implementation detail and not meant to be used
> directly.
>
> Perhaps if you can explain why you need to know if a symbol is bound or
> unbound, we might be able to help you better achieve your goal.
>
> Cheers,
> Peter


reply via email to

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