The inspiration for this post comes from this request:
Can someone explain this Kotlin expression.
Abhinav Kulshreshtha γ» Oct 5 '19
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)
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.
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?
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.
You may be interested by this:
github.com/dbacinski/Design-Patter...
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?
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 parameterDialogBuilder
and returningUnit
. But instead you define the parameter as an extension function ofDialogBuilder
.kotlinlang.org/docs/reference/lamb...
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
What Problem(s) does solve Kotlin better than other languages?
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
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()
}