Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return value from the function in kotlin

Tags:

kotlin

I am trying to return the boolean value from the function.

fun  validateDetails(jabberId:String, passwordText: String) {

            if(jabberId.isEmpty()){
                jabber_id.requestFocus()
                jabber_id.error="Jabber id can't be null."
                 return false
            }else if(jabberId.isBlank()){
             jabber_id.requestFocus()
             jabber_id.error="Jabber id can't be blank."
              return false
            }else if (passwordText.isNotEmpty()){
                password.requestFocus();
                password.error="Password can't be null."
                 return false
            }
             else{
                 return true
            }   

    }

Error: The boolean literal does not conform to the expected type Unit.

I know unit is the default return type in kotlin. How will i change this to boolean.

like image 848
Ankur Khandelwal Avatar asked Apr 08 '17 14:04

Ankur Khandelwal


People also ask

How do you return a value from a function?

To return a value from a function, you must include a return statement, followed by the value to be returned, before the function's end statement. If you do not include a return statement or if you do not specify a value after the keyword return, the value returned by the function is unpredictable.

Can I return 2 values from a function in Kotlin?

Kotlin has generic Pair and Triple types that can return two or three values from the function.

Can you return in Kotlin?

Returns and jumps Kotlin has three structural jump expressions: return by default returns from the nearest enclosing function or anonymous function. break terminates the nearest enclosing loop. continue proceeds to the next step of the nearest enclosing loop.


1 Answers

Kotlin can only infer the returned type of a function when its a expression, so if your function has a body, you need to specify the returned type after the function paramameters

fun functionName(param: Type...): ReturnedType { 
    //function body
}

fun  validateDetails(jabberId:String, passwordText: String):Boolean {

            if(jabberId.isEmpty()){
                jabber_id.requestFocus()
                jabber_id.error="Jabber id can't be null."
                 return false
            }else if(jabberId.isBlank()){
             jabber_id.requestFocus()
             jabber_id.error="Jabber id can't be blank."
              return false
            }else if (passwordText.isNotEmpty()){
                password.requestFocus();
                password.error="Password can't be null."
                 return false
            }
             else{
                 return true
            }   

    }

As glee8e mentioned, this could be done using an expression. This is how it'd be done.

fun  validateDetails(jabberId:String, passwordText: String) = when {
    jabberId.isEmpty() -> {
        jabber_id.requestFocus()
        jabber_id.error="Jabber id can't be null."

        false
    }

    jabberId.isBlank() -> {
        jabber_id.requestFocus()
        jabber_id.error="Jabber id can't be blank."

        false
    }

    passwordText.isNotEmpty() -> {
        password.requestFocus();
        password.error="Password can't be null."
        false
    }

    else -> true  
}
like image 112
Jefferson Tavares Avatar answered Jan 04 '23 05:01

Jefferson Tavares