Kotlin何时(配对),还有其他方法吗?
我有一个Kotlin何时(配对),还有其他方法吗?,kotlin,Kotlin,我有一个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() COP
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()
}
使用to
对构造函数是唯一的方法吗?这似乎是一个奇怪的使用对(虽然它的工作)。我很难找到它,因为代码片段
for ((key, value) in hashMap) {
println("$key $value)
}
让我觉得我应该能够在代码中做类似的事情,例如
when (activeRequest.verb, activeRequest.resourceType) {
(GET, "all") -> allGet()
(PUT, "foo") -> fooPut()
...
else -> logMismatch()
}
当这两个人工作的时候。。。如果我想做3项呢?循环示例中的语法是a,它基本上是一种语法糖,用于在一行中声明对对象的多个成员变量的引用。它不会反过来,因为Kotlin没有用于任意元组的机制
我真的想不出一个漂亮的方法来处理两个以上的变量。我考虑的选项是使用基本上作为元组工作的:
enum class Response(val verb: String, val type: String) {
GET_FOO("GET", "foo"),
...
INVALID("?", "?");
companion object {
fun from(verb: String, type: String): Response {
for(response in values()) {
if(response.verb == verb && response.type == type)
return response
}
return INVALID
}
}
}
when(Response.from(activeRequest.verb, activeRequest.resourceType)) {
GET_FOO -> getFoo()
...
}
或者使用数组。不幸的是,Kotlin数组的相等性不是由内容决定的,因此最终会产生大量的样板文件,并且当
语法不再看起来很好时,。(我添加了一个扩展函数,使它变得更好,但我仍然不喜欢它):
fun数组。whenCheat(vararg其他:有吗?):布尔值{
返回此内容与其他内容相同
}
val array=arrayOf(“GET”、“foo”)
什么时候{
array.whenCheat(“GET”,“foo”)->getFoo()
...
}
我的怀疑是,如果有一张功能响应图,这种事情会更好。希望其他人能提供更聪明的解决方案。另一种选择是使用数据类。例如:
data class Response(val verb: String, val type: String, val other: Int)
// This is an example of what the functions could be... edit as needed
val all = { _: Response -> "all"}
val some = { _: Response -> "some"}
val unknown = { _: Response -> "unknown"}
val handlers = mapOf<Response, (Response) -> String>(
Response("GET", "all", 200) to all,
Response("GET", "some", 400) to some
// and all your other mappings
)
如果我想做3件事呢
使用Triple
而不是Pair
如果您只是将数据捆绑在一个类中并重写其equals
方法,则可以在时使用任意多的项:
class Person(
var firstName: String = "",
var lastName: String = "",
var age: Int = 0,
var rate: Int = 0) {
override fun equals(other: Any?): Boolean {
val otherPerson = other as Person
return (
firstName.equals(otherPerson.firstName) &&
lastName.equals(otherPerson.lastName) &&
age.equals(otherPerson.age) &&
rate.equals(otherPerson.rate))
}
// just to avoid warning
override fun hashCode(): Int {
return super.hashCode()
}
}
fun main(args: Array < String > ) {
val p = Person("John", "Doe", 18, 2)
when (p) {
Person("Nick", "Doe", 18, 2) -> println("found at 1")
Person("John", "Doe", 18, 2) -> println("found at 2")
Person("Maria", "Doe", 18, 2) -> println("found at 3")
else -> println("not found")
}
}
班级人员(
var firstName:String=“”,
var lastName:String=“”,
变量年龄:Int=0,
风险值比率:Int=0){
覆盖乐趣等于(其他:任何?):布尔值{
val otherPerson=其他人作为人
返回(
firstName.equals(otherPerson.firstName)&&
lastName.equals(otherPerson.lastName)&&
年龄。等于(其他人。年龄)&&
费率等于(其他人费率))
}
//只是为了避免警告
重写哈希代码():Int{
返回super.hashCode()
}
}
趣味主键(args:Array){
val p=人(“约翰”,“多伊”,18,2)
何时(p){
Person(“尼克”,“多伊”,18,2)->println(“发现于1”)
Person(“John”,“Doe”,18,2)->println(“发现于2”)
Person(“Maria”,“Doe”,18,2)->println(“发现于3处”)
else->println(“未找到”)
}
}
这正是数据类擅长的地方:自动定义等于和hashCode
________
val myFun = handlers.getOrDefault(myResponse, unknown)
class Person(
var firstName: String = "",
var lastName: String = "",
var age: Int = 0,
var rate: Int = 0) {
override fun equals(other: Any?): Boolean {
val otherPerson = other as Person
return (
firstName.equals(otherPerson.firstName) &&
lastName.equals(otherPerson.lastName) &&
age.equals(otherPerson.age) &&
rate.equals(otherPerson.rate))
}
// just to avoid warning
override fun hashCode(): Int {
return super.hashCode()
}
}
fun main(args: Array < String > ) {
val p = Person("John", "Doe", 18, 2)
when (p) {
Person("Nick", "Doe", 18, 2) -> println("found at 1")
Person("John", "Doe", 18, 2) -> println("found at 2")
Person("Maria", "Doe", 18, 2) -> println("found at 3")
else -> println("not found")
}
}