我对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
}
现在我要为所有POST,PUT,PATCH,DELETE之类的HttpMethod编写一次:
fun HttpMethod.isWrite() =
when (this) {
DELETE, PUT, PATCH, POST -> true
else -> false
}
这有可能实现吗?
答案 0 :(得分:4)
有可能,您只需要导入这些符号即可,例如:
import com.example.HttpMethod.DELETE
import com.example.HttpMethod.PUT
import com.example.HttpMethod.PATCH
import com.example.HttpMethod.POST