DEV Community

Send me all the Kotlin questions you were too afraid to ask

The inspiration for this post comes from this request:

I actually enjoy the challenge to try to explain useful things with simple words. Everyone can say complex sentences filled with buzzwords. Cloud-ready serverless blockchain-based something something. The real test is whether you can explain it with simple words. As a great french writer once said:

(français) Ce que l'on conçoit bien s'énonce clairement, Et les mots pour le dire arrivent aisément
(english) Whatever is well conceived can be clearly said. And the words to say it flow with ease.

So don't hesitate, if you have any Kotlin-related questions you were too afraid to ask, reply to this post.

Top comments (10)

Collapse
 
alexpi_31 profile image
Alejandro Pimentel

Isn't Kotlin just another iteration of languages like Groovy?

In other words, groovy didn't really replace Java in the end, although it found niche applications being gradle the most popular... I think. Maybe I'm wrong but... seems to me groovy will eventually fade away completely.

Do you think Kotlin might also face the same destiny? If not, why?

Ty.

Collapse
 
jmfayard profile image
Jean-Michel πŸ•΅πŸ»β€β™‚οΈ Fayard • Edited

Hello I don't really want to get into comparing Kotlin with other JVM languages that are interesting in their own right and that I don't know so well.

But on the core of your question, yes, I do think Kotlin can be seen as a replacement for Java. It certainly did it in the Android world, which is now Kotlin first. And the reasons why it did apply elsewhere too. In most cases where Java is a good solution, Kotlin is a great solution. Because of the effortless interopability, you can start adding a bit of Kotlin in your existing Java codebase, and then notice it's better, and eventually replace it completely. I don't want to write in Java anymore and I think that usually I won't have to very often. Writing a JVM library is the counter example that comes to mind. Here it could make sense to use Java so that people don't have a transitive dependency to the Kotlin standard library. But even that I think could become no more problematic than adding a dependency to guava if Kotlin continues to grows.

Of course Java (or JavaScript) are huge enough that they will never disappear. But I do think that for most new projects, using Kotlin (or typescript) will be seen as a practical solution that improve developer productivity, so why not using it?

Collapse
 
alexpi_31 profile image
Alejandro Pimentel

Thank you for your response. This makes sense.

Three weeks ago I started a side project in Kotlin that could potentially replace a legacy Java code I created a couple of years ago. I'm very curious on how easy it'll be to integrate it in the whole solution, as well as how the classic GoF patterns will be applied in Kotlin.

Cheers and thanks for sharing.

Thread Thread
 
jmfayard profile image
Jean-Michel πŸ•΅πŸ»β€β™‚οΈ Fayard

You may be interested by this:
github.com/dbacinski/Design-Patter...

Thread Thread
 
alexpi_31 profile image
Alejandro Pimentel

Hey, builder pattern is interesting....

I'm trying to understand this expression:

fun dialog(init: DialogBuilder.() -> Unit): Dialog =
DialogBuilder(init).build()

The part that is kind of weird to me is: DialogBuilder.()

Is that related to the extension functions?

Thread Thread
 
jmfayard profile image
Jean-Michel πŸ•΅πŸ»β€β™‚οΈ Fayard

So actually the builder pattern is integrated in Kotlin itself.

Given data class User(val id: Int = 1, val name: String = "")
Then this do the same as the builder pattern would do in Java
val user = User(name = "Alejandro", id =42)

The dialog function is a building block for building a DSL.
It takes as parameter a function init that you could define as having a parameter DialogBuilder and returning Unit. But instead you define the parameter as an extension function of DialogBuilder.

kotlinlang.org/docs/reference/lamb...

Collapse
 
zahash profile image
zahash • Edited

I wanted to check some constraints whenever a String is instantiated.

lets say, I have a Email typealias
typealias Email = String

and I have a validator function
fun throwIfInvalidEmail(email: Email): Email{
if (email.length < 3) throw Exception("too short")
return email
}

I currently can achieve it like this
var email: Email = throwIfInvalidEmail("asdf@asdf.com")

But I want to know if giving the validator function the name "Email" (same name as the typealias) instead of "throwIfInvalidEmail" is a good idea.

for Eg: (the first "Email" is typealias and the second "Email" is function)
var email: Email = Email("asdf@asdf.com")

the type of the variable email is still String. we are still validating if it is a valid email. But doing it this way seems to be closer to the business domain

Collapse
 
b0nuspunkt profile image
Bonuspunkt

What Problem(s) does solve Kotlin better than other languages?

Collapse
 
jmfayard profile image
Jean-Michel πŸ•΅πŸ»β€β™‚οΈ Fayard • Edited
  • Kotlin is the child of JetBrains, a company that produces some of the best IDE, especially for Java.
  • So JetBrains obsession since its creation has been to understand how it can help programmers writing better software, to discover detect and fix all the common errors that programmers do. They have been very good at it, but at some point you reach the limit of what you can do in the IDE.
  • Why not integrate all this learning in the programming language itself? Well improving Java itself is seriously difficult, most of the things you should improve, you cannot for backwards compatibility reasons. Or you can reinvent a totally new language, but if you cut yourself from the massively important Java ecosystem, then it's not very attractive.
  • Can we have the best of both worlds? Yes. Jetbrains wrote a new language with a compiler that produces Java bytecode and is effortlessly compatible with the rest of the JVM ecosystem.
  • I think it was C++ inventor who said that you have two kind of languages: the ones people complain about a lot and the ones nobody uses. There is a lot of truth to this: think JavaScript (lots of flaws, very widespread) vs Elm (very nice language, few can use it). But the false dichotomy always made me sad. Can't we have more more well designed language that are also not confined to a niche? Well that's Kotlin.

What does Kotlin better than the others? It's not a single thing, there is no silver bullet as Frederic Brooks pointed out long ago. It's the compound effect of the lessons learned watching programmers do mistake, not being afraid to copy what other programming languages do well and having the expertise to do so (because jetbrains build IDE for many languages), being a practical choice, providing great tooling, being safe and concise, having good documentation, a very interesting community, and fixing a lots of small issues.

No silver bullet - essence and accidents in software engineering

Collapse
 
berlin_kick profile image
berlin_kick • Edited

I have a list of cars and each id of car can have multiple color values, i want to combine them such that each color object will have set of color options..

but i have this exception:
GroupedFlux allows only one Subscriber Suppressed: java.lang.Exception: #block terminated with an error

How can i achieve what i want? Can you please help me out?

Flux.fromIterable(carItems)
.groupBy { it.id }
.flatMap { mapToCar(it) }

private fun mapToCar(cars: GroupedFlux): Flux {
return cars.take(1).map {
Car(
id = it.id,
colors = mapToColors(cars)
)
}
}

private fun mapToColors(cars: Flux): Set {
return cars.map {
CarColor(
color = it.color
)
}.collect(Collectors.toSet())
.map { it.toImmutable() }
.block() ?: emptySet()
}