Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift's struct in Kotlin (or Java)? Multiple instances of the same class, mutating its variables

I am not sure if I am clear with my title. In Swift I have this struct.

struct Movement {
 let name: String
 var reps: Int
}

let movement1 = Movement(name: "Push Ups", reps: 20)

var movementA = movement1
movementA.reps = 30

print(movement1.reps) // Prints 20
print(movementA.reps) // Prints 30

What is alternative in Kotlin (or Java)? The problem is that in my app I have a workout that consists of multiple movements. I append these movements to the MutableList and the problem appears when I have workout where a movement is repeated.

For example when I want to enter workout like:

20 Push Ups

30 Pull Ups

40 Push Ups

I end up with:

40 Push Ups

30 Pull Ups

40 Push Ups

At first, I select movement #1 and set reps of movement #1 but when I select movement #3 and set reps of movement #3 it overrides variables of movement #1.

Any idea?

I have tried something like this but it didn't work.

class Movement(name: String) {
 var reps: Int? = null
}
val movement1 = Movement("Push Ups")
movement1.reps = 20
var movementA = movement1
movementA.reps = 30
Log.i("REPS1: ", movement1.reps.toString()) // 30
Log.i("REPSA: ", movementA.reps.toString()) // 30
like image 670
Ondřej Ševčík Avatar asked Apr 11 '18 21:04

Ondřej Ševčík


People also ask

Why struct is faster than class Swift?

Rather than a copy, a reference to the same existing instance is used. Structures and classes in Swift have many things in common. The major difference between structs and classes is that they live in different places in memory. Structs live on the Stack(that's why structs are fast) and Classes live on Heap in RAM.

Which keyword enables a method of a struct to change the value of the struct itself in Swift?

As the "The Swift Programming Language" iBook states: By default, the properties of a value type cannot be modified from within its instance methods. And so to make this possible we can declare methods with the mutating keyword inside structs and enums.

What is struct in Kotlin?

In Java and hence Kotlin there are no structs. Instead, to store data you simply create a class that has the properties corresponding to the data you want to store. For example: class MyPersonClass{ var firstName:String=""

Why are structs immutable in Swift?

So then, why is it said that structs are immutable? Structs are copied on mutation. That is, the original is not mutated. This is in terms of observable effects.


2 Answers

I have never used Swift, but it seems like you're having a mutable value which gets copied when you assign it to a new variable. In Kotlin, you cannot have such type. When you assign a value to a new variable, only the reference gets copied and it still points to the same exact memory location.

You could use a data class which gives you a convenient copy method:

data class Movement(val name: String, val reps: Int)

val movement1 = Movement("Push Ups", 20)
val movementA = movement1.copy(reps = 30)
like image 133
Bubletan Avatar answered Nov 06 '22 14:11

Bubletan


First, let me cover why you have the same value for both movement1 and movementA. When you assign in Kotlin, you are copying a reference, not a class.

var movementA = movement1

So what that does is create a new variable called movementA and points it to the exact same object that movement1 is pointing to.

I think you probably want to look at data classes. They come with a copy constructor that might come in handy.

data class Movement(val name: String, val reps: Int)

And then you can create them off of each other, like in the swift code you show above.

val movement1 = Movement("Push-Ups", 20)
val movementA = movement1.copy(reps = 30)

Or you could make the properties vars instead of vals and mutate them directly...

data class Movement(var name: String, var reps: Int)
var movement1 = Movement("Push-Ups", 20)
var movementA = movement1.copy()
movementA.reps = 30
like image 22
Todd Avatar answered Nov 06 '22 14:11

Todd