https://kotlinlang.org logo
#feed
Title
# feed
s

Stephan Schröder

03/06/2023, 11:14 AM
So I wrote a new blog post about a language feature Java devs coming to Kotlin aren't familar with: inline classes. And since it's generally more engaging to provide a useful application of a feature, I showcase a little Password inline class that improves security in at least two different ways. https://void2unit.onrender.com/post/inline+password/
👍 6
👌 2
👍🏾 1
j

Johann Pardanaud

03/06/2023, 12:03 PM
Really cool usage of
toString
for the password, thank you!
😊 1
c

CLOVIS

03/06/2023, 2:12 PM
What if the original String value is needed
Another solution: developers can just locally replace the
toString
by one that actually prints the value during debugging, to log the actual values (don't forget to remove it before review!)
Like the annotation
@JvmInline
is hinting at, the only Kotlin backend that supports inline classes is the JVM.
I thought this too, but that's not true!
@JvmInline
is a future-proofing solution by the Kotlin team until Valhalla comes out, because they want a simple un-annotated
value class
to be inlined through Valhalla, and it's probably going to be binary-incompatible. They have already started implementing value classes on other platforms (at least, Kotlin/JS IR already inlines value classes in some cases, without needing an annotation).
s

Stephan Schröder

03/06/2023, 2:40 PM
@CLOVIS your first comment: when I wrote "when the String value is really needed" I meant in production code. what you're saying isn't wrong, it's just a bit strange to call it "another solution" because you're clearly thinking about a different problem. (my solution is about not logging the password, and your solution is about logging the password) your second comment: I was already aware that future value classes are supposed to be implemented by Valhalla on the JVM. But the post is about what is available in Kotlin now. When you write "they have alread started implementing vallue classes on other platforms", do you mean that that possiblility is life already in Kotlin 1.8.10 in at least one other backend? Because if it isn't ("started implementing" doesn't sound very life) then "the only Kotlin backend that supports inline classes is the JVM" still stands. If value classes are indeed life on other backends, I'll update my text.
c

CLOVIS

03/06/2023, 3:43 PM
Sorry, when I meant is it's another solution to "But what if the original String value is needed? Then you use normal property access syntax: `password.value`". Yes, I mean that, right now, Kotlin/JS inlines value classes in some cases, without needing to use any annotations. I haven't found documentation on exactly what is and isn't implemented, though.
s

Stephan Schröder

03/06/2023, 3:54 PM
interesting! let me set up a Kotlin/JS project for the first time ever to verify this 🤓
you are correct,
value class
works on KotlinJs! I'm going to update the post 👍
c

CLOVIS

03/06/2023, 6:00 PM
I don't know about other platforms, but it's possible it works on Native too. The annotation is only needed on the JVM in case Valhalla is better.
s

Stephan Schröder

03/06/2023, 11:32 PM
I removed the entry about value classes only working on the JVM. Thanks for the correction 👍
5 Views