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

Support Kotlin inline/value classes #1170

Open
pswaminathan opened this issue Jul 6, 2020 · 10 comments
Open

Support Kotlin inline/value classes #1170

pswaminathan opened this issue Jul 6, 2020 · 10 comments
Milestone

Comments

@pswaminathan
Copy link

pswaminathan commented Jul 6, 2020

Suppose I have the following:

inline class Distance(val millimeters: Long): Comparable<Distance> {
    (truncated)
}

@JsonClass(generateAdapter = true)
data class ScratchEntity(val id: Int, val distance: Distance)

This compiles and works fine. But if I give any property a default value, e.g.

inline class Distance(val millimeters: Long): Comparable<Distance> {
    (truncated)
}

@JsonClass(generateAdapter = true)
data class ScratchEntity(val id: Int = 1, val distance: Distance)

I get the following traceback when trying to deserialize, e.g. {"id": 10, "distance": 20000}:

Exception in thread "main" java.lang.NoSuchMethodException: mypackage.ScratchEntity.<init>(int, mypackage.Distance, int, kotlin.jvm.internal.DefaultConstructorMarker)
	at java.lang.Class.getConstructor0(Class.java:3082)
	at java.lang.Class.getDeclaredConstructor(Class.java:2178)
	at mypackage.ScratchEntityJsonAdapter.fromJson(ScratchEntityJsonAdapter.kt:59)
	at mypackage.ScratchEntityJsonAdapter.fromJson(ScratchEntityJsonAdapter.kt:19)
	at com.squareup.moshi.JsonAdapter.fromJson(JsonAdapter.java:43)
	at org.jetbrains.kotlin.idea.scratch.generated.ScratchFileRunnerGenerated$ScratchFileRunnerGenerated.<init>(tmp.kt:24)
	at org.jetbrains.kotlin.idea.scratch.generated.ScratchFileRunnerGenerated.main(tmp.kt:30)

I suspect this is an interaction with inline classes: if I change to val distance: Int, all is good.

LMK if there is anything I can do to help diagnose or clarify!

@ZacSweers
Copy link
Collaborator

inline classes are not supported as they're not stable in kotlin itself yet

@pswaminathan
Copy link
Author

Sure, and I'm not suggesting this requires an immediate fix—I have a work-around I can use for now for my own use cases. But as inline classes are an extremely useful feature, and ostensibly the goal is to support them as they become stable in Kotlin, it is worth tracking and understanding how, as inline classes evolve and become stable, this would/could be addressed. No?

@swankjesse
Copy link
Collaborator

Yep, when Kotlin has inline classes we’ll find a way to make ’em work nicely with Moshi.

@swankjesse swankjesse changed the title [1.9.2] NoSuchMethodException when deserializing class with inline-class values and default values Support Kotlin inline classes Sep 18, 2020
@swankjesse swankjesse added this to the Icebox milestone Sep 26, 2020
@moomoomoo309
Copy link
Contributor

Heads up, Kotlin 1.5 is stabilizing inline classes, and the stable functionality is available in 1.4.30 via a build.gradle switch.

@z3ntu
Copy link

z3ntu commented May 5, 2021

@ZacSweers
Copy link
Collaborator

We should probably try to match how kotlinx-serialization treats them: https://youtrack.jetbrains.com/issue/KT-28791

PRs are welcome once #1339 merges and we update KotlinPoet. We will want to support this in both reflection and code gen, doesn't have to be the same PR but ideally we can reuse tests.

@ZacSweers ZacSweers changed the title Support Kotlin inline classes Support Kotlin inline/value classes May 6, 2021
@ZacSweers
Copy link
Collaborator

kotlin-reflect itself doesn't support these either, so I think it will be awhile. https://youtrack.jetbrains.com/issue/KT-27598

@k163377
Copy link
Contributor

k163377 commented Jul 2, 2022

Sharing regarding an update to a related issue.
The following issues have been resolved in kotlin-reflect 1.7.

https://youtrack.jetbrains.com/issue/KT-27598
https://youtrack.jetbrains.com/issue/KT-31141
https://youtrack.jetbrains.com/issue/KT-51804

@johnnyzen
Copy link

Any updates on supporting Kotlin UINT ? Or any workarounds known?

saket added a commit to saket/byte-size that referenced this issue Dec 25, 2023
Value classes aren't supported by moshi yet, which is a
blocker for my other projects.

square/moshi#1170
@SteinerOk
Copy link

Hey, @ZacSweers, do you have any plans or updates here?

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

No branches or pull requests

8 participants