Fading Coder

One Final Commit for the Last Sprint

Kotlin Exception Handling and Control Flow

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 Class Delegation and Property Delegation Mechanisms

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 {...

Kotlin Advanced Concepts: Lambda With Receiver, Wildcards, Where Clause, and Delegates

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...

Configuring Multiple GraphQL Endpoints with Apollo-Kotlin in Spring Boot

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...

Exception Handling in Kotlin Coroutines

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...

Working with Extensions for Kotlin Classes and Objects

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...

Kotlin Reflection: Complete Guide to Runtime Class and Function References

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...

Implementing Type-Safe Builders in Kotlin Using Function Literals with Receivers

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....

Kotlin Top-Level Functions and Properties for Java Interoperability

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...

Asynchronous Data Streams with Kotlin Flow

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():...