Exception Hierarchy In Kotlin, all exceptions inherit from the Throwable class. Each exception contains a message, stack trace, and an optional cause. Exceptions are thrown using the throw expression: throw RuntimeException("Something went wrong") Try-Catch Blocks Exception handling uses t...
Kotlin supports class-level delegation natively through the by keyword, eliminating boilerplate associated with manual forwarding. When a class implements an interface via delegation, all public members of that interface are automatically routed to the specified delegate object. interface Renderer {...
1. Lambda with Receiver package com.example.lambdawithreceiver fun <T> T.executeBlock(block: T.() -> Unit) { this.block() } fun <T> runWithParameter(receiver: T, block: T.() -> Unit) { receiver.block() } fun main() { 5.executeBlock { println(this * 3) } runWithParameter("Hello...
Project Structure A typical Spring Boot project setup for Apollo-Kotlin with multiple GraphQL endpoints includes the folloiwng directoyr layout: . ├── build.gradle.kts └── src ├── main │ ├── graphql │ │ ├── endpointA │ │ │ ├── SampleQuery.graphql │ │ │ └── schema.json │ │ └── endpointB │ │ ├── Anoth...
This section covers exception handling and cancellation in the context of exceptions. We already know that cancelling a coroutine throws a CancellationException at suspension points, which is ignored by the coroutine mechanism. Here, we'll explore what happens when exceptions are thrown during cance...
Kotlin allows you to add new functionality to an existing class without inheriting the target class or relying on structural patterns like the Decorator pattern. This capability is implemented via a feature called extensions. For example, you can add new functions to third-party libray classes that...
Class References The fundamental reflection capability involves obtaining runtime references to Kotlin classes. To retrieve a reference to a statically known Kotlin class, use the class literal syntax: val customClass = MyClass::class Kotlin class references differ from Java class references. To obt...
Type-safe builders in Kotlin enable the creation of domain-specific languages (DSLs) for constructing hierarchical data structures in a semi-declarative manner. This approach leverages well-named functions as builders combined with function literals that have receivers, ensuring static type safety....
When Kotlin files are compiled, top-level functions declared within them are converted into static methods. These methods are housed in a class named after the Kotlin file with a Kt suffix. For instance, a file named MathUtils.kt will produce a class named MathUtilsKt. // MathUtils.kt package com.ex...
A suspending function returns a single value asynchronously. To return multiple computed values asynchronously, Kotlin provides the Flow type. Representing Multiple Values Collections in Kotlin can represent multiple values. For example, a function that returns a list of numbers: fun fetchNumbers():...