Search code examples
spring-bootkotlinenumshttp-method

Writing when cases in kotlin


i'm relatively new to kotlin and im loving it. I'm trying to re-write an when function, but I'm having trouble even figuring out what to google.

This was my starting code:

fun HttpMethod.isWrite() =
    when (this) {
        HttpMethod.DELETE -> true 
        HttpMethod.PUT -> true
        HttpMethod.PATCH -> true
        HttpMethod.POST -> true
        else -> false
    }

I found out that it was possible to also write it like this:

fun HttpMethod.isWrite() =
    when (this) {
      HttpMethod.DELETE, HttpMethod.PUT, HttpMethod.PATCH, HttpMethod.POST -> true
      else -> false
    }

Now what I want is to only have to write HttpMethod once for all POST,PUT, PATCH, DELETE like:

fun HttpMethod.isWrite() =
    when (this) {
      DELETE, PUT, PATCH, POST -> true
      else -> false
    }

Is this possible to achive?


Solution

  • It is possible, you just have to import those symbols, eg:

    import com.example.HttpMethod.DELETE
    import com.example.HttpMethod.PUT
    import com.example.HttpMethod.PATCH
    import com.example.HttpMethod.POST