Hayan Ibrahim
Hayan Ibrahim

Reputation: 13

There is no output from the following code

class Car {
    constructor()
    public var doors:Int=2
}
fun main(args: Array<String>)  {
    fun printCar(car: Car) {
        println(car?.doors)
        val isCoupe = car.let {
            (it.doors <= 2)
        }
        if (isCoupe==true) {
            println("Coupes are awesome")
        }
        else{print("not coupes")}
    }
}

I was expecting an output

i think the problem is when calling an object

Upvotes: 1

Views: 36

Answers (1)

Daniel T
Daniel T

Reputation: 1179

Kotlin gives you freedom to put your functions wherever you want. Putting one function inside another is more or less the same as putting it outside. Unlike what it looks like, Kotlin does not call it by default. You need to call it by adding printCar(Car()) at the end of main().

class Car {
    constructor()
    public var doors:Int=2
}
fun main(args: Array<String>)  {
    fun printCar(car: Car) {
        println(car?.doors) // There is a warning here but that's besides the point
        val isCoupe = car.let {
            (it.doors <= 2)
        }
        if (isCoupe==true) {
            println("Coupes are awesome")
        }
        else{print("not coupes")}
    }
    val myCar = Car()
    printCar(myCar)
}

Output:

2
Coupes are awesome

Upvotes: 2

Related Questions