在反引号内转义双引号

时间:2019-08-08 08:45:59

标签: kotlin

有没有办法在方法名称中使用双引号呢?

@Test
fun `should do "something"`() {
    // ...
}

它适用于',但不适用于"。有没有一种方法可以转义双引号?

(由于语法错误,我收到了编译错误)

1 个答案:

答案 0 :(得分:2)

如果使用反引号进行转义,则可以在方法名称中使用特殊字符。 您的示例在我的Kotlin项目中编译。 但这也取决于目标平台,如果编译为Android,则字符集的限制更大。

在此处查看讨论-https://discuss.kotlinlang.org/t/more-characters-allowed-for-identifiers-than-grammar-specifies-what-is-supported/2359/11

这里的语法定义-https://kotlinlang.org/docs/reference/grammar.html#Identifier