Professional Documents
Culture Documents
Decoding Kotlin - Your Guide To Solving The Mysterious in Kotlin
Decoding Kotlin - Your Guide To Solving The Mysterious in Kotlin
Your guide to
solving the
mysterious in
Kotlin
Nullability
Whenever possible?
CRUD Entity Example
@Test
Is this possible? fun `should mysteriously get a list with a
car part with a name null`() {
carPartDao.findAll()
.filter { it.name == null }
.shouldHaveSize(1)
}
Reflection Example
@JvmStatic @JvmStatic
public static final void main(@NotNull String[] args) { public static final void main(@NotNull String[] args) {
Intrinsics.checkNotNullParameter(args, "args");
Intrinsics.checkNotNullParameter(args, "args");
SpecialShopLocalReturn this_$iv = INSTANCE;
SpecialShopNonLocalReturn this_$iv = INSTANCE; int $i$f$goToStore = false;
int $i$f$goToStore = false; String var3 = "Walks in";
String var3 = "Walks in"; System.out.println(var3);
System.out.println(var3); int var4 = false;
int var4 = false; String var5 = "Make purchase";
String var5 = "Make purchase"; System.out.println(var5);
String var6 = "Walks out";
System.out.println(var5);
?
System.out.println(var6);
} }
Decompiled
code
Since the late 50’s TCO was already
a theory intentend to be applied to
Tail Recursivity. It allows tail
recursive functions to be
transformed into iterative functions
in the compiled code for better
performance.
Tail Call
Optimization
What is the catch?
Tail Call Optimization
Tail recursive
Double) : Part {
override val totalWeight: Double
return acc
get() = weight }
} val part = parts.first()
data class ComplexCarPart( val remainingParts = parts.drop(1)
val name: String, val currentWeight = acc + part.totalWeight
val weight: Double, return when (part) {
override val parts: List<Part>
) : is ComplexPart -> totalWeight(remainingParts +
ComplexPart { part.parts, currentWeight)
override val totalWeight: Double
get() = weight
else -> totalWeight(remainingParts, currentWeight)
} }
}
data class Car(
val name: String,
override val parts: List<Part>
) : ComplexPart {
}
override val totalWeight: Double
get() = parts.sumOf { it.totalWeight } All variables
are immutable
Tail Call Optimization
https://kotlinlang.org/docs/annotations.html#annotation-use-site-targets
@Target({ElementType.METHOD,
ElementType.FIELD,
ElementType.ANNOTATION_TYPE,
ElementType.CONSTRUCTOR,
ElementType.PARAMETER,
ElementType.TYPE_USE})
PARAMETE
@Retention(RetentionPolicy.RUNTIME) R
@Repeatable(List.class)
@Documented is
@Constraint(
validatedBy = {} selected
)
public @interface Size {
Working with Data classes
@delegate:DelegateToCarHorn
val carHorn: Horn by SoundDelegate(CarHorn())
}
Working with Delegates
@NotNull
public final Horn getWagonHorn() {
return this.wagonHorn$delegate.getValue(this, $$delegatedProperties[0]);
}
@NotNull
public final Horn getCarHorn() {
return this.carHorn$delegate.getValue(this, $$delegatedProperties[1]); No Horn!
}
}
Working with Delegates
@Service
data class DelegationService(
val id: UUID = UUID.randomUUID()
) {
@delegate:LocalDateTimeValidatorConstraint
@get: Past
val currentDate: LocalDateTime by LocalDateTimeDelegate()
}
@Past
@NotNull
public LocalDateTime getCurrentDate() {
return this.currentDate$delegate.getValue(this, $$delegatedProperties[0]);
}
What’s next?
➔ Better understanding of the Kotlin Language.
➔ Don’t fight the Spring Framework or anything else like Quarkus. They are not evil and they are not magic.
➔ Read the Kotlin documentation and only use Google as a last resort.
➔ Nothing is perfect and Kotlin also falls into that category and recognizing that, allow us to be better.
Thank you!
Questions?
Resources
● Null Safety: https://kotlinlang.org/docs/null-safety.html
● Inline: https://kotlinlang.org/docs/inline-functions.html
● Tail Call Optimization: https://kotlinlang.org/docs/functions.html#tail-recursive-functions
● Annotation use-site targets: https://kotlinlang.org/docs/annotations.html#annotation-use-site-targets
● Spring Validation via AOP : https://docs.spring.io/spring-framework/reference/web/webmvc/mvc-controller/ann-validation.html
Source code and slides
● https://github.com/jesperancinha/kotlin-mysteries
● Scribd: https://www.scribd.com/presentation/726367924/Decoding-Kotlin-Your-Guide-to-Solving-the-Mysterious-in-Kotlin
● Slide-share: https://www.slideshare.net/slideshow/decoding-kotlin-your-guide-to-solving-the-mysterious-in-kotlinpptx/267506251
About me
● Homepage - https://joaofilipesabinoesperancinha.nl
● LinkedIn - https://www.linkedin.com/in/joaoesperancinha/
● YouTube - JESPROTECH
■ https://www.youtube.com/channel/UCzS_JK7QsZ7ZH-zTc5kBX_g
■ https://www.youtube.com/@jesprotech
● X - https://twitter.com/joaofse
● GitHub - https://github.com/jesperancinha
● Hackernoon - https://hackernoon.com/u/jesperancinha
● DevTO - https://dev.to/jofisaes
● Medium - https://medium.com/@jofisaes
See you
Next time!
See you
Next time!