Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

182
Vistas
Simpler way to write x == 1 && y == 1 && z == 1 in Kotlin?

I was wondering if there is a simpler or shorter way to write repetitive conditions like x == 1 && y == 1 && z == 1?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You could consider using predicates using all. For example:

listOf(x, y, z).all {it == 1}

This will return true when x==1, y==1, and z==1.

If the goal is to shorten what you want, there's not much legroom as your boolean expression is already very concise. Some of the other answers have illustrated how to make what you want more readable, however.

over 4 years ago · Santiago Trujillo Denunciar

0

When it is (exactly) repeated code, you should consider to extract the statement into a method and give a meaningfull name, which you know from the context where it is used. This makes the requirement (reading the code) easier to understand, at the point where it is used. And it makes it also easier to spot the it is always the same condition.

if (conditionName(x, y, z)) {

}

fun boolean conditionName(int x, int y, int z) {
    return x == 1 && y == 1 && z == 1;
}

I cannot think of a shorter statement for the condition, but a method extraction will improve your code readability, which should be your overall goal.

over 4 years ago · Santiago Trujillo Denunciar

0

You can make a convenience function for this kind of repeated code (or if you just need it to be more readable, or safer to edit):

fun <T> T.allEqual(vararg items: T) = items.all { it == this }

// or you could make the receiver the first parameter instead, this is just
// more explicit about which item is the value being compared against
1.allEqual(x, y, z)

But no, there isn't any other shorthand built into the language as far as I'm aware - conditions like x == 1 chained with boolean operators are about as simple as it can get! If you want to check multiple things without repeating yourself, any and all make that easy, while being flexible for all kinds of situations, and allowing the user to put together the functionality they need with those more general functions.

If you specifically want a version of all that does a simple comparison to a single value, and doesn't require creating an iterable like listOf, you have to write your own with those tools (which is basically what I've done). It's up to you if you think it's worth it!

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda