代码之家  ›  专栏  ›  技术社区  ›  Travis Griggs

Kotlin什么时候(配对<>),还有其他方法吗?

  •  3
  • Travis Griggs  · 技术社区  · 6 年前

    我有一个 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件事怎么办?

    4 回复  |  直到 6 年前
        1
  •  3
  •   emerssso    6 年前

    destructuring declaration

    enum

    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()
        ...
    }
    

    when

    fun Array<*>.whenCheat(vararg others: Any?): Boolean {
        return this contentEquals others
    }
    
    val array = arrayOf("GET", "foo")
    when {
       array.whenCheat("GET", "foo") -> getFoo()
       ...
    }
    

        2
  •  1
  •   marianosimone    6 年前

    data class

    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
    )
    

    val myFun = handlers.getOrDefault(myResponse, unknown)
    
        3
  •  0
  •   user8320224    6 年前

    Triple Pair

        4
  •  0
  •   user8959091    6 年前

    when 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")
        }
    }