Method gen:custom-write of a struct not working within lists

24 views
Skip to first unread message

Dimaugh Silvestris

unread,
Mar 4, 2021, 11:44:23 AM3/4/21
to Racket Users
I have a struct defined as:
(struct ugen sc.unit (name rate inputs) 
  #:methods gen:custom-write
  [(define write-proc
     (make-constructor-style-printer
      [λ (x) (let [(rate (ugen-rate x))
                   (name (symbol-append (ugen-name x) ':))]
               (cond [(eq? rate 'ar) name]
                     [else (symbol-append
                            name rate)]))]
      [λ (x) (ugen-inputs x)]))] #:transparent)

So that (ugen 'sinosc 'ar (1 2)) prints as (sinosc: 1 2), which incidentally it's the same as the function call that creates that value, that is: (sinosc: 3 4) creates the struct instance (ugen 'sinosc 'ar (3 4)). *

The problem is, within a list it prints like '(#<sinosc:: 1 2>), which is quite ugly.

* I don't know if this is a smart choice. I'm building a SuperCollider client, and in SuperCollider there's hundreds of ugens (unit generators, such as oscillators, filters, etc).
Because there's hundreds of them, I thought it might be heavy on memory or cpu to declare a specific struct for each one of them; so I choose instead to declare only constructors that build different instances of the ugen struct.

Sam Tobin-Hochstadt

unread,
Mar 4, 2021, 12:56:12 PM3/4/21
to Dimaugh Silvestris, Racket Users
I think you want to add `#:property prop:custom-print-quotable 'never`
to that struct declaration, and then it will behave as you wanted.

Sam
> --
> You received this message because you are subscribed to the Google Groups "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/56b85a49-2b64-465a-a58d-3b758aa823c5n%40googlegroups.com.

Yury Bulka

unread,
Mar 4, 2021, 2:52:28 PM3/4/21
to Dimaugh Silvestris, racket...@googlegroups.com
Wow, so great to hear the a SuperCollider client in Racket is on the horizon:)

--
Yury Bulka
https://mamot.fr/@setthemfree
#NotOnFacebook

Dimaugh Silvestris

unread,
Mar 4, 2021, 3:24:28 PM3/4/21
to racket...@googlegroups.com
On Thu, 4 Mar 2021 at 19:29, Sam Tobin-Hochstadt <sa...@cs.indiana.edu> wrote:
It doesn't print that way because that wouldn't turn back into the original value when evaluated, since it's quoted. 

Is there any other way? 
If not, I might consider the possibility of having a struct type for each of those ugens, but as I was saying in my first mail - is that sensible? That would mean declaring ~600 struct types.

On Thu, 4 Mar 2021 at 20:52, Yury Bulka <setth...@privacyrequired.com> wrote:
Wow, so great to hear the a SuperCollider client in Racket is on the horizon:)

Well, slowly and from the hands of an amateur programmer. But if this mailing list and the SuperCollider mailing list are patient enough with me, we might have one in a few months :)  
So far, SynthDef compilation is mostly finished. I haven't gotten into OSC messaging yet, which is the other big half.

Sam Tobin-Hochstadt

unread,
Mar 4, 2021, 3:31:00 PM3/4/21
to Dimaugh Silvestris, Racket Users
On Thu, Mar 4, 2021 at 3:24 PM Dimaugh Silvestris
<dimaughs...@gmail.com> wrote:
>
> On Thu, 4 Mar 2021 at 19:29, Sam Tobin-Hochstadt <sa...@cs.indiana.edu> wrote:
>>
>> It doesn't print that way because that wouldn't turn back into the original value when evaluated, since it's quoted.
>
>
> Is there any other way?
> If not, I might consider the possibility of having a struct type for each of those ugens, but as I was saying in my first mail - is that sensible? That would mean declaring ~600 struct types.

I'm not totally sure what you want. The goal of `print` is to print
things in roughly a way that evaluating the printed result would
produce the original value. Thus:

Evaluating '(1 2 3) produces a 3-element list.
Evaluating (sinosc: 1 2) sounds like it produces a structure that you want.

But evaluating '(sinosc: 1 2) produces a 3-element list, not a
structure. Thus, you don't want that as the printed result. That means
that if you want to construct a pair of 0 and that structure, you need
to use `cons` and not `quote`.

If you want something else then you might need to construct the
function provided for `gen:custom-write` directly, instead of using
`make-constructor-style-printer`, but I still don't understand what
you want the result to be.

Sam
Reply all
Reply to author
Forward
0 new messages