Regex or Wildcard in Kotlin's when statement?

我与影子孤独终老i 提交于 2019-12-10 13:26:01

问题


I'm working on a RESTful app in Kotlin and for the router, I'm using a when statement, as it's the most readable and good looking conditional.

Is there a way to use Regex or a wildcard in the when statement for a string?

(So that URIs like "/article/get/" would all be passed to the same controller)

The structure of my router is as follows:

when(uri) {
    "some/url" -> return SomeController(config).someAction(session)
}

回答1:


Yes.

import kotlin.text.regex

val regex1 = Regex( /* pattern */ )
val regex2 = Regex( /* pattern */ )
/* etc */

when {
    regex1.matches(uri) -> /* do stuff */
    regex2.matches(uri) -> /* do stuff */
    /* etc */
}

You could also use containsMatchIn if that suits your needs better than matches.

Explanation:

The test expression of a when statement is optional. If no test expression is included, then the when statement functions like an if-else if chain, where the whenCondition of each whenEntry shall independently evaluate to a boolean.


EDIT:

So I thought about it for awhile, and I came up with a different approach that might be closer to what you want.

import kotlin.text.regex

when (RegexWhenArgument(uri)) {
    Regex(/* pattern */) -> /* do stuff */
    Regex(/* pattern */) -> /* do stuff */
    /* etc */
}

Where RegexWhenArgument is minimally defined as:

class RegexWhenArgument (val whenArgument: CharSequence) {
    operator fun equals(whenEntry: Regex) = whenEntry.matches(whenArgument)
    override operator fun equals(whenEntry: Any?) = (whenArgument == whenEntry)
}

This approach lets you get as close as possible to the "argument-ful" when expression syntax. I think it's about as streamlined and readable as it can be (assuming that you define the RegexWhenArgument class elsewhere).

This approach uses something similar to the visitor design pattern in combination with Kotlin's operator overloading to redefine what constitutes a "match" between a when expression argument and a whenEntry. If you really wanted to, I suppose you could take this approach a step further and generify RegexWhenArgument into a general-purpose WhenArgument and WhenArgumentDecorator that allows you to specify custom "match" criteria in a when expression for any sort of type, not just Regex.



来源:https://stackoverflow.com/questions/41270732/regex-or-wildcard-in-kotlins-when-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!