Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Kotlin does not allow slash in identifiers

The Unicode is allowed in identifiers in backticks

val `💾id` = "1"

But slash is not allowed

val `application/json` = "application/json"

In Scala we can have such names.

like image 405
Combo Avatar asked Mar 19 '18 08:03

Combo


2 Answers

This is a JVM limitation. From the specification section 4.2.2:

Names of methods, fields, local variables, and formal parameters are stored as unqualified names. An unqualified name must contain at least one Unicode code point and must not contain any of the ASCII characters . ; [ / (that is, period or semicolon or left square bracket or forward slash).

In Scala names are mangled to avoid this limitation, in Kotlin they are not.

like image 165
Todd Sewell Avatar answered Sep 27 '22 16:09

Todd Sewell


Kotlin's identifiers are used as-is, without any mangling, in the names of JVM classes and methods generated from the Kotlin code. The slash has a special meaning in JVM names (it separates packages and class names). Therefore, Kotlin doesn't allow using it in an identifier.

like image 26
yole Avatar answered Sep 27 '22 18:09

yole