Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign same value to multiple variable in Kotlin

Tags:

kotlin

I have two variables and want to assign same value to both the variables at the same time something like below:

var allGood: Boolean = false
val deviceId: String = "3550200583"
var isValidId: Boolean = false
allGood = isValidId = deviceId.length > 0 && deviceId.length <= 16

is there any way to achieve this?

like image 720
Muneesh Avatar asked May 30 '19 05:05

Muneesh


People also ask

How to assign a type to a variable in Kotlin?

So in Kotlin, variable types are deduced automatically at compile time (as opposed to runtime). However, if you want to specify the type of a variable, you can do so using a colon (:), followed by the type you wish to assign, as we did in examples two and three above. val name: String = "Phil" val age: Int = 27

Can you declare multiple properties on the same line in Kotlin?

"Declaring multiple properties on the same line is frowned upon by many Java style guides, so we did not implement support for that in Kotlin." Note that his answer is more than 4-years old, so there could have been changes since then. Even in Java, is declaring multiple variables in one line really bad practice?

What is the difference between Var and Val in Kotlin?

The difference between var and val is that variables declared with the var keyword can be changed/modified, while val variables cannot. Unlike many other programming languages, variables in Kotlin do not need to be declared with a specified type (like "String" for text or "Int" for numbers, if you are familiar with those).

What is the difference between component1 and component2 in Kotlin?

That is, the component1 function is the first destructured variable, the component2 function is the second variable, and so on. In Kotlin, a few types are already usable for destructuring declarations: The data classes, as they declare component functions for all their properties


3 Answers

Because assignment is not an expression in Kotlin, you can't do multiple assignments that way.  But there are other ways.  The most obvious is simply:

isValidId = deviceId.length > 0 && deviceId.length <= 16
allGood = isValidId

A more idiomatic (if longer) way is:

(deviceId.length > 0 && deviceId.length <= 16).let {
    allGood = it
    isValidId = it
}

(By the way, you can simplify the condition to deviceId.length in 1..16.)

There are a couple of reasons why Kotlin doesn't allow this.  The main one is that it's incompatible with the syntax for calling a function with named parameters: fn(paramName = value).  But it also avoids any confusion between = and == (which could otherwise cause hard-to-spot bugs).  See also here.

like image 178
gidds Avatar answered Oct 18 '22 02:10

gidds


What about:

var allGood: Boolean = false
val deviceId: String = ...
val isValidId: Boolean = (deviceId.length in 1..16).also { allGood = it }

.also allows you to perform additional operations with the value that it receives and then returns the original value.

like image 24
E.M. Avatar answered Oct 18 '22 02:10

E.M.


Another way is to do it like this:

val deviceId: String = "3550200583";
val condition = deviceId.length > 0 && deviceId.length <= 16
var (allGood, isValidId) = arrayOf(condition, condition);
like image 1
Gal Naor Avatar answered Oct 18 '22 02:10

Gal Naor