hallvard
05/29/2018, 1:41 PMwhen
cleans up my code. Used with in
, checking if an element is in a collection is very simple. Now have I overlooked the possibility of doing it the other way around? I.e. is it possible to do something like this (pseudo-code):
when (collection-of-strings) {
has "string 1" -> handleString1()
has "string 2" -> handleString2()
has "string 3" -> handleString3()
else -> throw Exception()
}
fred.deschenes
05/29/2018, 1:46 PMwith(collection-of-strings) {
when {
contains("string 1") -> handleString1()
contains("string 2") -> handleString2()
contains("string 3") -> handleString3()
else -> throw Exception()
}
}
not as clean thoughorangy
05/29/2018, 1:46 PMwhen
first match wins, and semantics here is not clear. Should it “handle” every element in a collection, or just first match?contains
hallvard
05/29/2018, 1:49 PMif {} else if ...
that we did in java ...! Thanks for pointing it out!fred.deschenes
05/29/2018, 1:49 PMhallvard
05/29/2018, 1:50 PMorangy
05/29/2018, 1:52 PMswitch
with invokedynamic
for Java: http://mail.openjdk.java.net/pipermail/amber-spec-observers/2018-April/000568.html
This can be done manually in your case by using something like
when(listOfStrings.indexOfWhich("a", "b", "c")) {
0-> // a
1-> //b
}
indexOfWhich
can be implemented efficiently, but there is no such function in stdlib currently