Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

630
Visualizações
Kotlin: Convert variables to non nullable if condition is true

What I mean is following.

Consider this code:

// Can be a Long or null
val data1= param1.toLongOrNull()
val data2= param2.toLongOrNull()
val dataN= paramN.toLongOrNull()

// Proceed with the action if ALL of the data are not nulls
if(notNull(data1, data2, dataN)){
    // do something with data1,2,N
}

notNull() is a utility function that accepts a list of variable arguments. Returns true if all of the arguments are not nulls.

fun <T> notNull(vararg elements: T): Boolean {
    elements.forEach {
        if (it == null) {
            return false
        }
    }
    return true
}

The issue I am having is that Kotlin does not know that inside the if block, the data1,2,N cannot be null. As a result, this fails to compile:

if(notNull(data1, data2, dataN)){
     data1 + data2 + dataN
     // Fail: Required Long, Found Long?
     // Operator + not allowed on nullable receiver.
}

On the other hand, if the variables are explicitly checked against null, then Kotlin knows this is fine and does not complain.

if(data1!=null && data2!=null && dataN!=null){
     data1 + data2 + dataN
}

It would be nice to "configure" the notNull method in such way, so that Kotlin knows that once it returns true, any of the passed parameters cannot be null inside the block.
Is this possible?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Obviously multiple ways to skin a cat, but you could do something like this :

inline fun <T> doIfNotNull(elements: Array<T?>, block : (Array<T>) -> Unit) {
    val validElements = arrayListOf<T>()
    for (i in elements) {
       if (i == null) return
       validElements.add(i)
    }
    block.invoke(validElements.toArray(elements))
}

Using with varargs (has to be last parameter, making the receiver function first arg, which doesn't feel as nice);

inline fun <T> doIfNotNull(block : (Array<T>) -> Unit, vararg elements : T?) {
    val validElements = arrayListOf<T>()
    for (i in elements) {
       if (i == null) return
       validElements.add(i)
    }
    block.invoke(validElements.toArray(elements))
}

Example :

fun test() {
    val elements = arrayOf(1L, 2L, null)
    doIfNotNull(elements, {
        it.sum()
    })
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda