Skip to content

Use Kotlin's Inline value classes for custom scalars #6332

Open
@x-sheep

Description

Use case

Inline value classes are a thin wrapper over other types, which can potentially be erased entirely by the compiler.

value class ID(val value: String)

The above is safer to use in Client code, since it's not possible to assign a String to an ID or vice versa. It also discourages (but doesn't prevent) string manipulation by the client, which is useful for scalars that are documented to be opaque.

Describe the solution you'd like

The various shortcuts like mapScalarToKotlinLong, mapScalarToKotlinString etc. could be expanded to take an additional argument asValueClass: Boolean which will generate a value class instead of a typealias.

I'm not sure if it's preferable to generate another Adapter for this new type, or if its usage should be applied inside every existing object adapter:

val id = ID(value = StringAdapter.fromJson(_, _)

val json = StringAdapter.toJson(_, _, id.value)

I think avoiding a new adapter would allow the compiler to inline the type in more cases and increase performance.

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions