Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading