# Partially override gen:custom-write for the display case only

**URL:** <https://racket.discourse.group/t/partially-override-gen-custom-write-for-the-display-case-only/3616>\
**Category:** Questions & Answers\
**Tags:** generics, struct\
**Created:** [March 10, 2025, 5:31pm UTC](https://racket.discourse.group/t/partially-override-gen-custom-write-for-the-display-case-only/3616 "2025-03-10T17:31:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rvdalen](https://avatars.discourse-cdn.com/v4/letter/r/b4bc9f/32.png) [@rvdalen](https://racket.discourse.group/u/rvdalen)\
**Post date:** [March 10, 2025, 5:31pm UTC](https://racket.discourse.group/t/partially-override-gen-custom-write-for-the-display-case-only/3616/1 "2025-03-10T17:31:57Z")

</div>

I am trying to customize the printing of a struct for the display case to something that displays more user-friendly but I am not sure how to express: "do the default action for print and write cases".

Here is the code I have:

```scheme
(define (service-print service port mode)
  (case mode
    ; WRITE-mode - I want to use the default printer behaviour
    [(#t)
     (write service port)]
    ; DISPLAY-mode - Use my custom behaviour
    [(#f)
     (fprintf port "#<service ~a>" (service.-id service))]
    ; PRINT-mode - I want to use the default printer behaviour
    [else
     (print service port mode)]))

(struct service.
  (id ; ^String
   opts ; ^HashTable
   )
  #:methods gen:custom-write 
  [(define write-proc service-print)])

```

The code above gives the following strange result for the print and write cases:

```scheme
> (display (service. "Transport" (hash)))
#<service Transport>

> (write (service. "Transport" (hash)))
#0=#0#

> (print (service. "Transport" (hash)))
#0=#0#

```

Why am I getting the strange `#0=#0#` result instead of the default write and print behaviour?

---

<div class="post-metadata">

**Author:** ![ryanc](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/ryanc/32/71_2.png) [@ryanc](https://racket.discourse.group/u/ryanc)\
**Post date:** [March 10, 2025, 10:22pm UTC](https://racket.discourse.group/t/partially-override-gen-custom-write-for-the-display-case-only/3616/2 "2025-03-10T22:22:42Z")

</div>

> [@rvdalen](#):
>
> I am not sure how to express: "do the default action for print and write cases".

Racket does not provide a way to do that, I believe. There's not much point, since the default would be just

```
(fprintf port "#<service>")

```

If you actually want to print more information, you could also have your printer fall back to a printer constructed using `make-constructor-style-printer` for some cases.

> [@rvdalen](#):
>
> Why am I getting the strange `#0=#0#` result instead of the default write and print behaviour?

That's how Racket prints cyclic data structures. For example, here's a cyclic "list" of ones:

```
> (require racket/shared)
> (shared ([ones (cons 1 ones)]) ones)
#0='(1 . #0#)

```

The `#0=` means "label the next value I print with the `0`", and the `#0#` means "this is a reference back to the enclosing value labeled with `0`"

When Racket prints a service object `s`, the Racket printer makes a note that it's currently printing `s`. When the custom printer for `s` calls `write` (or `print`) on `s` again, Racket detects the cycle and inserts the sharing notation. It's an abuse of the sharing notation (you can't `read` it, for example), but your printer is saying "To print me out, just print me out."

---

<div class="post-metadata">

**Author:** ![rvdalen](https://avatars.discourse-cdn.com/v4/letter/r/b4bc9f/32.png) [@rvdalen](https://racket.discourse.group/u/rvdalen)\
**Post date:** [March 11, 2025, 4:32am UTC](https://racket.discourse.group/t/partially-override-gen-custom-write-for-the-display-case-only/3616/3 "2025-03-11T04:32:59Z")

</div>

Thanks Ryan.

Maybe I should rethink my approach to this problem. I would like to display the  
abbreviated values such as:

```scheme
#<service Transport>

```

But changing the default printing behaviour by implementing `gen:custom-write` might not be what I should be doing. It does not feel like the right tool for the job?

I think I will try defining my own interface for my structs that are specifically used for the purpose of displaying abbreviated values and traversing child values of the graph.

Thanks for explaining the sharing notation. I will read up about that topic a bit more.
