this post was submitted on 02 Apr 2024
7 points (100.0% liked)
Kotlin
671 readers
2 users here now
Kotlin is a statically typed programming language for the JVM, Android, JavaScript, and native.
Subreddit rules:
- Be civil
- No spam
- Stay on-topic
- No fluff
Resources:
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
I'm not 100% familiar with Kotlin, but I think extension methods in Kotlin are the same as C#, basically syntactic sugar.
So you'd write an extension method like:
this
being the keyword making it an extension method - and you can call that function on an object by doingobject.DoSomething()
- Yes. But underneath it's the same as doingObjectExtension.DoSomething(object)
(A static invocation to yourObjectExtension
class andDoSomething
method.So on the surface it looks like you're injecting a new method into your DTO, and your DTO is not a pure data object anymore, but actually you're just creating an helper function that's statically invoked - that looks like you can call it "on the object" but actually you're not.
As for whether it's a good / common practice to create mappers like that in Kotlin, I don't really know. I do it often in C# though.
I was suspecting something like that. I'll be looking into it a bit more.
I also notice some difference in what's considered "best practice" between Java and C#, so I'm not sure what's actually best practice haha. Probably somewhere in the middle, I suppose.