Decoding Kotlin - Your Guide To Solving The Mysterious in Kotlin
Decoding Kotlin - Your Guide To Solving The Mysterious in Kotlin
Kotlin - Your
guide to
solving the
mysterious in
Kotlin
By João Esperancinha 2024/04/24
https://www.meetup.com/dutch-kotlin-user-group/events/
300229414/
Topics for today
Nullability
Whenever
possible?
CRUD Entity Example
@Test
Is this fun `should mysteriously get a list with a
car part with a name null`() {
possible? carPartDao.findAll()
.filter { it.name == null }
.shouldHaveSize(1)
}
Reflection Example
matter?
Crossinline as just a marker
@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);
} }
Decompil
ed
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
Tail Call code for better
performance.
Optimization
What is the
catch?
Tail Call Optimization
Why use
val totalWeight: Double
}
sealed interface ComplexPart : Part{ tailrec fun totalWeight(parts: List<Part>, acc: Double =
}
val parts: List<Part>
}
override val totalWeight: Double
get() = parts.sumOf { it.totalWeight } All
variables
are
Tail Call Optimization
and
((ComplexPart)part).getParts());
}
acc = currentWeight;
parts = var10000;
}
} else {
acc = currentWeight;
parts = remainingParts;
algorithm is
}
}
iterative
}
Kotlin provides use-site
targets that allow us to
specify where particular
annotations have to be
applied. Sometimes we
need them and sometimes
we don’t
Data classes
Why?
and
Frameworks
Working with Data classes
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
● Bluesky - https://bsky.app/profile/jesperancinha.bsky.social
● Mastodon - https://masto.ai/@jesperancinha
● 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!