Bug in Kotlin

For some reason I’m able to access a property that hasn’t been declared as so.

class FuelTank (
    var currentlevel: Double = 0.0,
    var minFuel : Double = currentlevel * 10 / 100) {
    var lowFuel by Delegates.vetoable(0) {
        _, _, new ->
        if (new < minFuel ) { println("Fuel Tank too low"); true }
        else { false }
    }
}

The property in question is the lowFuel one.

A property that hasn’t been declared as what?

lowFuel has been declared as a property. Its type hasn’t been specified, but the compiler infers that from the return type of vetoable(), which in turn is inferred from the type of the initial value (0, an Int).

Why shouldn’t you be able to access it?

1 Like

Actually, your code declares lowFuel, but does not access (read/write) it :stuck_out_tongue_winking_eye: What do you mean exactly by saying that lowFuel hasn’t been declared?

It’s been declared, it just hasn’t been declared as a property.

Why not? var used in the class body declares its property.

What do you expect your var lowFuel by Delegates.vetoable(0) { ... } code does, if it doesn’t declare a property?

2 Likes

Remove { at the end of the line and it will work :smiley:

This { is required there - it marks the beginning of the class body. It is just formatted in a confusing way.

3 Likes

Yeah, you are correct, my mistake.

This. All members of a kotlin class are public by default unless declared private. Making lowFuel a top level declaration in your class without specifying the visibility makes it a class property accessible outside the class.

1 Like