Hey I was wondering if there was a better way of breaking out of an if clause instead of doing this.
Copy code
if(true) run {
val example = null
if(example == null) {
return@run
}
}
if(true) run {
return@run
}
i
igor.wojda
03/16/2023, 9:31 AM
What about elvis operator?
val example = null ?: return@run
igor.wojda
03/16/2023, 9:33 AM
BTW the same code could be improved - it is hard to answer you question. Perhaps put real code and place some comments to better demonstrate problem
t
Tech
03/16/2023, 7:37 PM
Hey, sorry for the late response.
The main issue I'm having is setting up guard clauses for my if conditions, my workaround was to wrap the if clause in a run {} so I can just return out of the run
Tech
03/16/2023, 7:41 PM
Copy code
fun example(): List<String> {
val conf = /* load configuration */
if(conf.isSet("team_spawnpoint")) {
val section = conf.getSection("team_spawnpoint.spawns")
?: return@run
}
if(conf.isSet("all_spawnpoint")) {
val section = conf.getSection("all_spawnpoint.spawns")
?: return@run
}
return /*list of all valid spawn points*/
}
Something like this where my function can possibly load spawn points for a specific team and can also load spawn points for people unassigned to a team, if the section in the configuration doesn't exist I'd like to just break out of the if clause.