Hey I was wondering if there was a better way of b...
# getting-started
t
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
What about elvis operator?
val example = null ?: return@run
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
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
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.