Deep Kotlin tutorial covering contracts, tailrec, compiler hint, recursion with examples, diagram, mistakes, and interview answers.

Contracts and Tail Recursion

What is Contracts and Tail Recursion?

Contracts and Tail Recursion is an important Kotlin topic from 1.4 Inline, Reified, and Advanced Language Features. In this lesson we focus only on the concepts that belong together: contracts, tailrec, compiler hint, recursion. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas.

Why students should learn it

  • Understand Contracts and Tail Recursion in simple language
  • Use contracts in real Kotlin or Android code
  • Explain Contracts and Tail Recursion in interviews with examples and trade-offs

How it works

Contracts and Tail Recursion: contracts, tailrec, compiler hint, recursion works by giving Kotlin extra control over instance creation, visibility, and compiled output. object and companion object model a single shared instance, visibility modifiers decide what other files or modules can see, and features such as inline, reified, and destructuring remove boilerplate while the compiler still generates ordinary, predictable bytecode underneath.

  1. Identify what problem Contracts and Tail Recursion: contracts, tailrec, compiler hint, recursion solves.
  2. Write the smallest Kotlin snippet that demonstrates it.
  3. Add one Android or JVM use case where it matters.
  4. Compare it with a similar concept so the difference is clear.
  5. Finish by naming one common mistake and the safer alternative.
Interview tip: keep the answer simple first, then add example, trade-off, and mistake.

Types / Classification

  • contracts: core concept in this lesson
  • tailrec: core concept in this lesson
  • compiler hint: core concept in this lesson
  • recursion: core concept in this lesson
  • object: a class with exactly one instance
  • companion object: members callable through the class name
  • internal: visible anywhere in the same module
  • inline function: body copied into the call site by the compiler

Important differences

Concept 1Concept 2How to remember it
classobjectclass can have many instances; object always has exactly one.
privateinternalprivate is visible only in the same file or class; internal is visible anywhere in the same module.
Regular functioninline functionA regular function is a normal call; an inline function's body is inserted at the call site, avoiding lambda allocation.

Diagram

flowchart TD
A["1.4 Inline, Reified, and Advanced Language Features"] --> B["Contracts and Tail Recursion"]
B --> C["Why it exists"]
C --> D["How it works in Kotlin"]
D --> E["Android or JVM example"]
E --> F["Mistakes and interview answer"]

Examples

Companion object as a factory

class ApiClient private constructor(val baseUrl: String) {
companion object {
fun create(baseUrl: String) = ApiClient(baseUrl)
}
}
val client = ApiClient.create("https://api.skillupclass.com")

Destructuring a data class

data class Point(val x: Int, val y: Int)
val (x, y) = Point(4, 9)

When to use it

Use it when: Use these when building a small library or API that other developers will call, where the extra expressiveness genuinely reduces boilerplate for the caller.

Avoid it when: Avoid reaching for them in regular app feature code just because they are available - they add a learning cost for readers.

Common mistakes

  • Using a companion object for everything instead of plain top-level functions
  • Marking a function inline without a lambda parameter to justify it
  • Making everything public by default instead of choosing the narrowest visibility that works
  • Overusing destructuring on data classes whose field order can change
  • Reaching for a custom DSL when a simple function call would be clearer

Interview questions

Read each answer in four parts: simple answer, code example, trade-off, and common mistake.

1. What is Contracts and Tail Recursion in 1.4 Inline, Reified, and Advanced Language Features?

Answer: Contracts and Tail Recursion is an important Kotlin topic from 1.4 Inline, Reified, and Advanced Language Features. In this lesson we focus only on the concepts that belong together: contracts, tailrec, compiler hint, recursion. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas.

class ApiClient private constructor(val baseUrl: String) {
companion object {
fun create(baseUrl: String) = ApiClient(baseUrl)
}
}
val client = ApiClient.create("https://api.skillupclass.com")
  • Trade-off: class and object solve related but different problems: class can have many instances; object always has exactly one.
  • Common mistake: Using a companion object for everything instead of plain top-level functions

2. How does Contracts and Tail Recursion work in Kotlin or Android code?

Answer: Contracts and Tail Recursion: contracts, tailrec, compiler hint, recursion works by giving Kotlin extra control over instance creation, visibility, and compiled output. object and companion object model a single shared instance, visibility modifiers decide what other files or modules can see, and features such as inline, reified, and destructuring remove boilerplate while the compiler still generates ordinary, predictable bytecode underneath.

data class Point(val x: Int, val y: Int)
val (x, y) = Point(4, 9)
  • Trade-off: class and object solve related but different problems: class can have many instances; object always has exactly one.
  • Common mistake: Marking a function inline without a lambda parameter to justify it

3. What is one common mistake with Contracts and Tail Recursion?

Answer: A common mistake is: Using a companion object for everything instead of plain top-level functions Another is: Marking a function inline without a lambda parameter to justify it

class ApiClient private constructor(val baseUrl: String) {
companion object {
fun create(baseUrl: String) = ApiClient(baseUrl)
}
}
val client = ApiClient.create("https://api.skillupclass.com")
  • Trade-off: class and object solve related but different problems: class can have many instances; object always has exactly one.
  • Common mistake: Using a companion object for everything instead of plain top-level functions

4. How would you explain Contracts and Tail Recursion with a small example?

Answer: Contracts and Tail Recursion is an important Kotlin topic from 1.4 Inline, Reified, and Advanced Language Features. In this lesson we focus only on the concepts that belong together: contracts, tailrec, compiler hint, recursion. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas. Here is a short example that shows it in practice.

class ApiClient private constructor(val baseUrl: String) {
companion object {
fun create(baseUrl: String) = ApiClient(baseUrl)
}
}
val client = ApiClient.create("https://api.skillupclass.com")
  • Trade-off: class and object solve related but different problems: class can have many instances; object always has exactly one.
  • Common mistake: Marking a function inline without a lambda parameter to justify it

5. What is the trade-off of using Contracts and Tail Recursion?

Answer: class and object solve related but different problems: class can have many instances; object always has exactly one.

data class Point(val x: Int, val y: Int)
val (x, y) = Point(4, 9)
  • Trade-off: class and object solve related but different problems: class can have many instances; object always has exactly one.
  • Common mistake: Using a companion object for everything instead of plain top-level functions

Practice task

Write one Kotlin example for Contracts and Tail Recursion. Then explain it using: what is it, how it works, where it is used, mistake to avoid, and one interview answer.