在 kotlin 中编写 when 案例
Writing when cases in kotlin
我对 kotlin 比较陌生,但我很喜欢它。我正在尝试重写一个 when 函数,但我什至无法弄清楚 google.
的内容
这是我的起始代码:
fun HttpMethod.isWrite() =
when (this) {
HttpMethod.DELETE -> true
HttpMethod.PUT -> true
HttpMethod.PATCH -> true
HttpMethod.POST -> true
else -> false
}
我发现也可以这样写:
fun HttpMethod.isWrite() =
when (this) {
HttpMethod.DELETE, HttpMethod.PUT, HttpMethod.PATCH, HttpMethod.POST -> true
else -> false
}
现在我想要的是只需编写一次 HttpMethod POST,PUT,PATCH,DELETE 就像:
fun HttpMethod.isWrite() =
when (this) {
DELETE, PUT, PATCH, POST -> true
else -> false
}
这有可能实现吗?
有可能,你只需要导入这些符号,例如:
import com.example.HttpMethod.DELETE
import com.example.HttpMethod.PUT
import com.example.HttpMethod.PATCH
import com.example.HttpMethod.POST
我对 kotlin 比较陌生,但我很喜欢它。我正在尝试重写一个 when 函数,但我什至无法弄清楚 google.
的内容这是我的起始代码:
fun HttpMethod.isWrite() =
when (this) {
HttpMethod.DELETE -> true
HttpMethod.PUT -> true
HttpMethod.PATCH -> true
HttpMethod.POST -> true
else -> false
}
我发现也可以这样写:
fun HttpMethod.isWrite() =
when (this) {
HttpMethod.DELETE, HttpMethod.PUT, HttpMethod.PATCH, HttpMethod.POST -> true
else -> false
}
现在我想要的是只需编写一次 HttpMethod POST,PUT,PATCH,DELETE 就像:
fun HttpMethod.isWrite() =
when (this) {
DELETE, PUT, PATCH, POST -> true
else -> false
}
这有可能实现吗?
有可能,你只需要导入这些符号,例如:
import com.example.HttpMethod.DELETE
import com.example.HttpMethod.PUT
import com.example.HttpMethod.PATCH
import com.example.HttpMethod.POST