Language design

More Deprecations Coming

There are two more items on the language cleanup list: backing-field syntax and static type assertion operator.

Backing Fields

When you have a property and you don’t implement at least one of its accessors (get or set) manually, such a property gets a backing field, i.e. a piece of storage holding its value:

var foo: Foo? = null
    set(v) {
        ...
    }
    // default getter is used

you can access the backing field through the name of the property prefixed with the $ sign:

$foo = 2

This may be needed to bypass the custom accessor(s).

This feature is rather rarely used, and also clashes visually with string templates ("$foo", surprisingly, has nothing to do with backing fields), so we want to get rid of it.

In case you really need it, your workaround is backing property:

private var _backing: Foo? = null
var foo: Foo?
    get() = _backing
    set(v) {
        ...
    }

Since no getters or setters are generated for private properties, the resulting byte code will be exactly the same.

Static Type Assertions

Another rarely used feature is the following syntax:

foo(bar, null: Baz)

The type after colon in an expression specifies the expected static type of it, i.e. this is not a cast, but simply an instruction to the compiler to make sure that the static type of this expression is actually “Bar”. The fact that it’s hard to explain has something to do with this being rarely used (I think Kotlin’s test data is the only major client). So, we are withdrawing this syntax, and maybe will make use of it later (possibilities include array/list slices and C-style ternary conditionals).

In case you needed this syntax to disambiguate your overloads, as is a good workaround.

image description