retrieving a structure's symbol name

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

retrieving a structure's symbol name

Marco Maggi-2
Ciao,

  I know  that it is  dirty, but is it  possible to retrieve  the symbol
name of  a structure  from the  block object?  If  I create  a structure
with:

   (define (make-it)
     (##sys#make-structure 'spiffy))

   (define O
     (make-it))

can  I extract  "spiffy"  from the  structure object  bound  to "O"?   I
searched "library.scm" in CHICKEN's 5.1.0 source without success.

TIA
--
Marco Maggi

_______________________________________________
Chicken-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/chicken-users
Reply | Threaded
Open this post in threaded view
|

Re: retrieving a structure's symbol name

Peter Bex
On Wed, Jul 24, 2019 at 03:49:13PM +0200, Marco Maggi wrote:

> Ciao,
>
>   I know  that it is  dirty, but is it  possible to retrieve  the symbol
> name of  a structure  from the  block object?  If  I create  a structure
> with:
>
>    (define (make-it)
>      (##sys#make-structure 'spiffy))
>
>    (define O
>      (make-it))
>
> can  I extract  "spiffy"  from the  structure object  bound  to "O"?   I
> searched "library.scm" in CHICKEN's 5.1.0 source without success.
You can access slot 0 to get it:

(##sys#slot (##sys#make-structure 'foo) 0) => foo

There's no official way to get at it.  But, if you are on CHICKEN 5.1
the define-record-type type name will contain it:

#;3> (define-record-type foo (make-foo) foo?)
#;4> foo  
foo

And it will be namespaced with the module if it is defined in a module.
But beware, we might change the representation of record types to be
objects instead of symbols at some point so we can add introspection
to records.

Cheers,
Peter

_______________________________________________
Chicken-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/chicken-users

signature.asc (499 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: retrieving a structure's symbol name

Marco Maggi-2
Peter Bex wrote:

> On Wed, Jul 24, 2019 at 03:49:13PM +0200, Marco Maggi wrote:
>> Ciao,

>>   I know  that it is  dirty, but is it  possible to retrieve  the symbol
>> name of  a structure  from the  block object?  If  I create  a structure
>> with:

>>    (define (make-it)
>>      (##sys#make-structure 'spiffy))

>>    (define O
>>      (make-it))

>> can  I extract  "spiffy"  from the  structure object  bound  to "O"?   I
>> searched "library.scm" in CHICKEN's 5.1.0 source without success.

> You can access slot 0 to get it:

> (##sys#slot (##sys#make-structure 'foo) 0) => foo

Yes!  Damn, I already knew about it...

> And it will be namespaced with the module if it is defined in a module.

Mh.   If I  understand correctly  this is  to avoid  collisions for  the
record printers,  and stuff like that.   If I define my  own record-type
constructor, and I want to  use the built-in record printers facilities:
is  CHICKEN expecting  me  to qualify  the name  with  the module?   The
symbols I want to use are expected to be unique...

Thanks.
--
Marco Maggi

_______________________________________________
Chicken-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/chicken-users
Reply | Threaded
Open this post in threaded view
|

Re: retrieving a structure's symbol name

Peter Bex
On Wed, Jul 24, 2019 at 07:22:45PM +0200, Marco Maggi wrote:
> Mh.   If I  understand correctly  this is  to avoid  collisions for  the
> record printers,  and stuff like that.

It is to avoid collisions for all accessors and predicates.  If I define
a record type X of 2 slots in module A, and in module B, I define a record
type also called X of 1 slot, then "X?" would return #t for both records.
And if I want to access the second slot of the record in module A, but
pass in an X of module B (which has only one slot), this will cause an
unsafe memory access.  That's because we can't safely check first if the
record is of the correct type.

>   If I define my  own record-type
> constructor, and I want to  use the built-in record printers facilities:
> is  CHICKEN expecting  me  to qualify  the name  with  the module?   The
> symbols I want to use are expected to be unique...

No, you can use unqualified symbols.  We still do that in core itself too,
for SRFI-4 vectors and for ports and so on.  But this is really low-level
stuff, and there's usually a better way.  What exactly are you trying to
accomplish?

Cheers,
Peter

_______________________________________________
Chicken-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/chicken-users

signature.asc (499 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: retrieving a structure's symbol name

Marco Maggi-2
Peter Bex wrote:

>>   If I define my  own record-type
>> constructor, and I want to  use the built-in record printers facilities:
>> is  CHICKEN expecting  me  to qualify  the name  with  the module?   The
>> symbols I want to use are expected to be unique...

> No, you can use unqualified symbols.  We still do that in core itself too,
> for SRFI-4 vectors and for ports and so on.  But this is really low-level
> stuff, and there's usually a better way.  What exactly are you trying to
> accomplish?

I am writing a library that mimics R6RS records.  Every R6RS record-type
can  be associated  to a  unique identifier  (UID), which  is a  symbol.
Defining the  same record-type  in multiple modules  is fine,  under the
correct conditions.

  Using the  UID as  struct type  name, and  so implementing  records as
simple CHICKEN structs, seems an efficient way to do it.
--
Marco Maggi

_______________________________________________
Chicken-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/chicken-users
Reply | Threaded
Open this post in threaded view
|

Re: retrieving a structure's symbol name

Peter Bex
On Thu, Jul 25, 2019 at 07:36:04AM +0200, Marco Maggi wrote:
> I am writing a library that mimics R6RS records.  Every R6RS record-type
> can  be associated  to a  unique identifier  (UID), which  is a  symbol.
> Defining the  same record-type  in multiple modules  is fine,  under the
> correct conditions.
>
>   Using the  UID as  struct type  name, and  so implementing  records as
> simple CHICKEN structs, seems an efficient way to do it.

I had a quick look at the R6RS page and god that shit is ugly :)
But you're right, using these low-level constructors seems the best way
to do this.  Maybe you can use gensyms or uuids for the record names.
uuids probably work better if you later want to generate a types database
for the library.

Cheers,
Peter

_______________________________________________
Chicken-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/chicken-users

signature.asc (499 bytes) Download Attachment