0
0
Kotlinprogramming~10 mins

Type constraints with upper bounds in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Type constraints with upper bounds
Define generic type T
Set upper bound: T must be subtype of SomeClass
Use T in function or class
Call function with argument of type T or subtype
Compile-time check: argument type fits upper bound?
Yes No
Run function
Function logic uses T safely
End
This flow shows how Kotlin checks that a generic type T respects an upper bound before running the function.
Execution Sample
Kotlin
fun <T : Number> doubleValue(value: T): Double {
    return value.toDouble() * 2
}

val result = doubleValue(10)
This function accepts any type T that is a subtype of Number and doubles its value.
Execution Table
StepActionType of TCheck Upper BoundResultOutput
1Call doubleValue with 10IntInt <: Number? YesPass
2Inside function: value.toDouble()IntN/AConvert 10 to 10.0
3Multiply 10.0 * 2DoubleN/AResult 20.0
4Return 20.0DoubleN/AFunction ends20.0
💡 Function ends after returning doubled value; type T confirmed as subtype of Number.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
valueN/A10 (Int)10 (Int)10 (Int)N/A
resultN/AN/AN/AN/A20.0 (Double)
Key Moments - 3 Insights
Why must the argument type be a subtype of Number?
Because the function declares <T : Number>, Kotlin enforces this at compile time (see execution_table step 1). Passing a type outside this bound causes a compile error.
What happens if we try to call doubleValue with a String?
The compiler rejects it immediately since String is not a subtype of Number, so the check in step 1 fails and the function does not run.
Why can we safely call toDouble() on value inside the function?
Because T is guaranteed to be a subtype of Number, which has the toDouble() method, so the call is safe (see step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the type of T at step 1?
AString
BInt
CDouble
DAny
💡 Hint
Check the 'Type of T' column in execution_table row for step 1.
At which step does the function convert the value to Double?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Action' and 'Result' columns in execution_table for step 2.
If we try to call doubleValue("text"), what happens?
AFunction runs and returns 0.0
BFunction runs but throws runtime error
CCompile error due to type constraint
DFunction runs and returns "texttext"
💡 Hint
Refer to key_moments about type constraint enforcement at compile time.
Concept Snapshot
fun <T : UpperBound> functionName(param: T) { ... }
- T must be subtype of UpperBound
- Compiler checks this before running
- Allows safe use of UpperBound methods on T
- Prevents passing incompatible types
- Enables flexible, type-safe generic code
Full Transcript
This visual trace shows how Kotlin uses type constraints with upper bounds in generics. The generic type T is declared with an upper bound Number, meaning T must be a subtype of Number. When calling the function doubleValue with an Int argument, Kotlin checks that Int is a subtype of Number and allows the call. Inside the function, it safely calls toDouble() on value because Number defines this method. The function doubles the value and returns a Double. If a type outside the bound, like String, is used, the compiler rejects the call. This ensures type safety and lets us write generic code that can use methods of the upper bound type.