8

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.

4
  • Remove the = before the curly brace. And please, indent your code properly. Commented Apr 8, 2017 at 14:55
  • @JBNizet removed. but still same error Commented Apr 8, 2017 at 14:57
  • 1
    Add the return type of the function: : Boolean. Commented Apr 8, 2017 at 15:00
  • @AnkurKhandelwal just added a litte explanation on what you need to do Commented Apr 8, 2017 at 15:03

1 Answer 1

16

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  
}
Sign up to request clarification or add additional context in comments.

4 Comments

Alternatively, you can convert this function body into a single expression, as it's just a big if expression.
Just updated my answer. Thank you for point that out. xD I'm still getting used with the power of Kotlin, so I didn't even think about that possibility
Since you are using kotlin, I assume you are also using IDEA (created by the JetBrains, the team behind kotlin), the power of which is also something you should pay attention to. If you set the pointer inside the method body (of your anwser), it will suggest you to change it into an expression body.
Yeah, I've been playing with Intellij IDEA for 1 1/2 month only, so I'm still geting used to it. I didn't know this trick by the way. Thanks for the advice :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.