class QueueExecutorImpl(
private val peripheral: Peripheral,
parentScope: CoroutineScope,
) : QueueExecutor {
I have a simple doubt. In the above class I have two parameters: one with
private val
and another without any modifier. My doubt is the following:
peripheral
is a copy and parentScope is a reference for the external object??
h
hho
06/26/2024, 1:17 PM
No, everything is references. The
val
only makes the class keep it as a field (available after the constructor ends).
👍 1
f
Francis Mariano
06/26/2024, 1:30 PM
Ok, if I use
val
and the reference is modified, the parameter of the class does not change because it is immutable, right? Otherwise, if
var
or none modifier is used the parameter changes if the reference is modified.
🚫 1
nono 1
s
Sam
06/26/2024, 1:39 PM
"the reference is modified"
This is not a thing in Kotlin. You can't modify a reference.
f
Francis Mariano
06/26/2024, 1:47 PM
I can not modify a reference, but I can modify its content, right? For example, it is possible cancel the scope of parentScope.
s
Sam
06/26/2024, 1:49 PM
Sure, yes. But there's no implicit copying. Whether you store something in a
val
property, a
var
property, or just use it as a constructor parameter, you're still referencing the original object.
Sam
06/26/2024, 1:50 PM
The difference between the two parameter styles in your example is the length of time the reference is held for; nothing else. No mutability or copying involved.