codegen-kotlin: Use same toString() representation for data classes and regular classes #223

Closed
opened 2025-12-30 01:22:28 +01:00 by adam · 2 comments
Owner

Originally created by @odenix on GitHub (Oct 21, 2024).

codegen-kotlin generates data classes whenever possible and regular classes otherwise.

For data classes, the default toString() implementation of data classes is used.
Example:

Person(name=Bob, address=Address(city=London))

For regular classes, a toString() method is generated whose output somewhat resembles Pkl syntax.
Example:

Person {
  name = Bob
  address = Address {
    city = London
  }
}

It would be more consistent to use the same toString() representation for data classes and regular classes.
This could be achieved in one of the following ways:

  1. Generate toString() methods for both data classes and regular classes.
  2. Generate toString() methods for regular classes whose output matches that of data classes.

(1) enables a toString() representation that somewhat resembles Pkl syntax, whereas (2) generates less code.

PS: codegen-java will face the same issue once it supports records.

Originally created by @odenix on GitHub (Oct 21, 2024). codegen-kotlin generates data classes whenever possible and regular classes otherwise. For data classes, the default `toString()` implementation of data classes is used. Example: ``` Person(name=Bob, address=Address(city=London)) ````` For regular classes, a `toString()` method is generated whose output somewhat resembles Pkl syntax. Example: ``` Person { name = Bob address = Address { city = London } } ``` It would be more consistent to use the same `toString()` representation for data classes and regular classes. This could be achieved in one of the following ways: 1. Generate` toString()` methods for both data classes and regular classes. 2. Generate `toString()` methods for regular classes whose output matches that of data classes. (1) enables a `toString()` representation that somewhat resembles Pkl syntax, whereas (2) generates less code. PS: codegen-java will face the same issue once it supports records.
adam closed this issue 2025-12-30 01:22:28 +01:00
Author
Owner

@bioball commented on GitHub (Oct 22, 2024):

Good call-out, and agreed that their rendering should be consistent.

My instinct here is to copy the rendering of Kotlin data classes and Java records. These are data objects in the target language, and we don't need to add any special behavior here.

@bioball commented on GitHub (Oct 22, 2024): Good call-out, and agreed that their rendering should be consistent. My instinct here is to copy the rendering of Kotlin data classes and Java records. These are data objects in the target language, and we don't need to add any special behavior here.
Author
Owner

@odenix commented on GitHub (Oct 22, 2024):

Agreed. I'll send a PR after the 0.27 release.

@odenix commented on GitHub (Oct 22, 2024): Agreed. I'll send a PR after the 0.27 release.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/pkl#223