Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert public API lists to arrays #1395

Draft
wants to merge 4 commits into
base: trunk
Choose a base branch
from

Conversation

drewhamilton
Copy link
Contributor

@drewhamilton drewhamilton commented Aug 9, 2023

Per Jake's comment on #1043. Split from #1392.

The arrays cause problems with some of the serialization tests: empty lists were omitted by default; empty arrays are included by default because different instances of an empty array are not equals.

Currently this PR half-implements a verbose solution – custom serializers for all serializable classes containing an array property. Happy to add the remaining custom serializers if this is the preferred solution. I'm hoping there's a less verbose way though.

Another option is to give these classes singleton default empty array values, but this is brittle because it's easy to write new code generation that doesn't know it should use these singletons. Also adds to the public API in a weird way.

.beginControlFlow("return %M", Stdlib.buildList)
.addStatement(
"this@%L.forEach { element -> add(element.%M(json)) }",
name,
schema.modifierToProtocol,
)
.endControlFlow()
.addCode("⇥.toTypedArray()⇤")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not convinced that just adding this actually helps perf since it's still building a list first. But I think to build an array directly, Modifier would need a size and an iterator.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We control Modifier so we could add behavior to expose the size if we need to.

)

private object EventSerializer : KSerializer<Event> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm yeah this is unfortunate. I initially envisioned that we would be able to do this with a property-local serializer. But I guess that's too late because the enclosing serializer for the type is what determines whether or not the value needs to be included. Once we're inside a serializer for the property we are always going to write out the value.

I'll look into this more. Maybe ask the serialization folks if they'd be open to a similar annotation to treat arrays as values and not references.

Another potential option in the future is to use a value class when custom equals gets supported. Then we could have a ValueBasedArray wrapper around an array. This assumes that it doesn't somehow incur boxing, which is not yet clear since there's no implementation of this to play with yet.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially envisioned that we would be able to do this with a property-local serializer. But I guess that's too late because the enclosing serializer for the type is what determines whether or not the value needs to be included.

Yep, that appears to be the case.

I filed Kotlin/kotlinx.serialization#2409, let's see what they think.

@JakeWharton JakeWharton added this to the 1.0.0 milestone Aug 17, 2023
@JakeWharton
Copy link
Collaborator

Marking this as blocked and for 1.0 (4-6 months away, minimum). Let's wait and see if serialization delivers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants