Ruckus
12/19/2018, 5:21 PMoperator fun contains
object Links {
<values here>
operator fun contains(link: String) = when (link) {
HELP, SHOP, BLOG, ... -> true
else -> false
}
}
Shawn
12/19/2018, 5:24 PMwhen
but similar issueRuckus
12/19/2018, 5:25 PMJake
12/19/2018, 5:25 PMShawn
12/19/2018, 5:25 PM.values()
but that’s problematic for other reasonsRuckus
12/19/2018, 5:26 PMkz
12/19/2018, 5:29 PMobject Links {
private val values = mutableSetOf<String>()
private operator fun String.unaryPlus(): String = this.also { values += it }
val HELP = +"https://..."
val SHOP = +"https://..."
val BLOG = +"https://..."
val GALLERY = +"https://..."
operator fun contains(string: String) = string in values
}
+
operator can be replaced with a more readable method invocation.Shawn
12/19/2018, 5:30 PMRuckus
12/19/2018, 5:31 PM+
(or some other function call), and means you can't have the values be const
.Jake
12/19/2018, 5:31 PMShawn
12/19/2018, 5:32 PM+
at the start of a constantkz
12/19/2018, 5:33 PMcontains
implementation. My snippet was just how you might handle the boilerplate.
@Ruckus definitely some downsides to using the +
. I was just going for something non-intrusive. The only alternative I can think of would require reflection or a lot more boilerplate (or enums as someone else mentioned)Shawn
12/19/2018, 5:33 PMin
check would return false, it’s just a simple equals
callJake
12/19/2018, 5:33 PMRuckus
12/19/2018, 5:35 PMkz
12/19/2018, 5:35 PMgsala
12/20/2018, 9:41 AMLinks.values().any { ... }