Reputation: 526
I'm working on an app which use a when statement
How can I make this more short?
when(page) {
0 -> poster[0].imageURL
1 -> poster[1].imageURL
2 -> poster[2].imageURL
3 -> poster[3].imageURL
else -> "image not provided"
}
Upvotes: 0
Views: 138
Reputation: 270860
You can just replace it with a range check:
if (page in 0..3) {
poster[page].imageURL
} else {
"image not provided"
}
Upvotes: 3