Soft Keywords

A soft keyword has a meaning in a particular context, and otherwise is available for use as an identifier.

For example, try appeared in the above list of hard keywords. catch and finally do not, and not because the author did not supply the complete list. catch and finally are considered to be soft keywords, as is import, which means that you can use them as identifiers in most places.

“Most places”, though, may not be everywhere.

So, for example, constructor is a soft keyword. You can have a variable named constructor, if you wanted:

fun main() {
  val constructor = 1337
  
  println(constructor)
}

However, at least on Kotlin/JS, you cannot have a function named constructor():

class Foo {
  fun constructor() {
    println("I'm not a constructor!")
  }
}

fun main() {
  Foo().constructor()
}

In this case, the problem appears to be tied to JavaScript interoperability, as the Kotlin/JS transpiler generates a function named constructor() that is the actual constructor, so having your own constructor() function causes a name collision.


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.