Serious question, not a troll. Javascript and Kotlin are two very different languages but it's always interesting to take a look at what other developer communities are doing.
If you know both languages, are there any particular aspects that you find Typescript does better than Kotlin?
Or if you are a front-end developer and don't know Kotlin, what are the parts of Javascript/Typescript where you find that the language is at best, concise, safe and eloquent?
The inspiration for this question comes from my last post, I wrote about a nice pattern I found for extracting constants in Kotlin
with(ConfigObject) {
println("Language $KOTLIN is sparking $JOY")
}
And I was blown away by a comment from KIP who reminded my that almost the same exact thing was possible in Javascript
with(ConfigObject) {
console.log(`Language ${TYPESCRIPT} is sparking ${JOY}`)
}
Kotlin is a very nice language, but also a relatively new one and there are still new useful patterns to discover. So I wondered: could I not accelerate this process by taking inspiration from Javascript's good parts?
I looked at the code produced by colleagues from the front-end, and one thing I like is object destructuring
const {id, firstName, lastName} = person
Kotlin also has object destructuring
val (id, firstName, lastName) = person
But in my experience, it's not used very much. One problem is that it is position-based. The code above may we working right now, but if someone changes the order of the properties in the Person
class, the code will fail. Typescript named-based approach is clearly better. Having realized that, I discovered a proposal to implement the same solution in Kotlin https://youtrack.jetbrains.com/issue/KT-19627
Good solution in the long term. But in the long term, we are all dead!
Can I somehow find a pattern that works now to implement something like what Typescript does? As it turns out: YES
Here I used again with
to transform what I need from my object into something I can easily destructure. It's much more general than what Kotlin offers by default, which works only for "the properties of a data class or a Tuple".