https://kotlinlang.org logo
#compiler
Title
# compiler
a

anlex N

03/03/2024, 3:12 PM
C++ father tell me that JAVA is built on top of C++, so what language is Kotlin built on? everyone.
d

dmitriy.novozhilov

03/03/2024, 8:05 PM

https://youtu.be/Ljr66Bg--1M?si=PpZNJ4JHv_yllBA0

a

anlex N

03/04/2024, 3:19 AM
when generate android apk, kotlin compile path is: Kotlin -> Java byte code -> apk?
👌 1
can Kotlin do "pass by reference" like C++?
d

dmitriy.novozhilov

03/04/2024, 7:37 AM
Everything is passed by reference by default
Exceptions are primitive types and value classes
a

anlex N

03/04/2024, 8:55 AM
can you update this [Gemini answer](https://gemini.google.com/app/f1ce571ade99913c)?
d

dmitriy.novozhilov

03/04/2024, 9:03 AM
AI is not reliable source of information
k

Klitos Kyriacou

03/04/2024, 9:37 AM
It's misleading to say "Everything is passed by reference by default". Kotlin is like Java when it comes to parameter passing. So, for example,
Copy code
fun foo(list: MutableList<String>) {
    list = arrayListOf<String>("bar")
}
If Kotlin was "pass by reference" in the same way as C++ and C#, then the above function
foo
would change the passed parameter
list
to a list containing an element "bar". But Kotlin is not pass-by-reference in this way. You can't change the value of a passed variable. I think what Dmitriy meant was that variables (except primitives) hold a reference to an object. So even though you pass the value of a variable to a function, that value is the reference of the object.
d

dmitriy.novozhilov

03/04/2024, 9:39 AM
References to parameters are immutable (non-reassignable) The code from your snippet won't compile
👀 1
a

anlex N

03/04/2024, 10:14 AM
@Klitos Kyriacou show me your code playground, please
@Klitos Kyriacou @dmitriy.novozhilov how to realize the same effort for "pass by reference" like C++ in the Kotlin?
k

Klitos Kyriacou

03/04/2024, 12:52 PM
You have to be explicit in Kotlin, using for example a wrapper object: let's say you want a function that changes a passed value, then you have to keep that value inside an object:
Copy code
class Wrapper<T>(var value: T)

fun foo(x: Wrapper<Int>) {
    x.value = 456
}

int main() {
    val x = Wrapper(123)
    foo(x)
    check(x.value == 456)
}
This is not idiomatic Kotlin. In Kotlin, we prefer to keep things immutable, so we would probably in the above case just write a function that takes one value and returns another value, rather than mutating.
a

anlex N

03/04/2024, 12:57 PM
as your code, x is like "pass by value" in the C++. x is "pass by value" in the C++ in the fact.
👌 1
this do not have performance. teacher.
what's solution?
a

Arjan van Wieringen

03/04/2024, 4:33 PM
Why doesn’t it have performance?
a

anlex N

03/06/2024, 11:09 AM
if no reference, it is performance problem.