我有一个when想要匹配两件事的构造:
when (activeRequest.verb to activeRequest.resourceType) {
GET to "all" -> allGet()
PUT to "foo" -> fooPut()
GET to "foo" -> fooGet()
POST to "bar" -> barPost()
GET to "bar" -> barGet()
COPY to "bar" -> barCopy()
DELETE to "bar" -> barDelete()
else -> logMismatch()
}
Run Code Online (Sandbox Code Playgroud)
使用to对构造函数是执行此操作的唯一方法吗?Pair 的使用似乎很奇怪(尽管它有效)。我很难找到它,因为代码片段像
for ((key, value) in hashMap) {
println("$key $value)
}
Run Code Online (Sandbox Code Playgroud)
让我觉得我应该能够在when代码中做类似的事情,例如
when (activeRequest.verb, activeRequest.resourceType) {
(GET, "all") -> allGet()
(PUT, "foo") -> fooPut()
...
else -> logMismatch()
} …Run Code Online (Sandbox Code Playgroud) kotlin ×1