https://kotlinlang.org logo
Title
i

Ifvwm

06/19/2019, 10:26 AM
I don't understand why kotlin is so different to others
g

ghedeon

06/19/2019, 10:34 AM
your
vv
is just a local variable
hope you don't expect local variables to be accesable from outside of the class? Otherwisie I'm afraid to ask about "others".
😂 1
i

Ifvwm

06/19/2019, 10:37 AM
instance variable, I don't know if there's this concept in kotlin
g

ghedeon

06/19/2019, 10:37 AM
probably you're looking for
class C<T>(v:T) {
    val vv = v
}
i

Ifvwm

06/19/2019, 10:38 AM
functions in class, they need to visit variables in the class
in python, we call self.var
g

ghedeon

06/19/2019, 10:38 AM
also, the
+
operation in your code is not defined for
T
, so
vv + vv
won't work.
#getting-started
i

Ifvwm

06/19/2019, 10:39 AM
T here is Int
m

marstran

06/19/2019, 10:40 AM
Not in your add function. There it's just
T
. You probably want this:
fun add(x: C<Int>, y: C<Int>): Int {
    return x.vv + y.vv
}
i

Ifvwm

06/19/2019, 10:43 AM
yes, that is what I want
but I don't know why T doesn't work here
c

cbruegg

06/19/2019, 10:44 AM
Python is very different from Kotlin when it comes to type-safety. You can do lots of stuff in Python that's not guaranteed to work in all cases and there's no compiler to scream at you that you're doing something unsafe
T
doesn't work there because it could be literally any type. You could invoke
add(C(false), C(false))
i

Ifvwm

06/19/2019, 10:47 AM
yeah, but T is made for polymorphic
pass string, sure, it may be error,
c

cbruegg

06/19/2019, 10:47 AM
Sorry, had a bad example, Boolean is a better example here
i

Ifvwm

06/19/2019, 10:47 AM
sneaky, string + string is ok
c

cbruegg

06/19/2019, 10:48 AM
You mean it should be an error for strings at runtime?
We don't want that, we want to prevent this at compile-time
We want to avoid functions that only work for selected inputs
It's actually easier this way: If you declare the function properly, you know what types of values are safe to call it with. In Python for instance it's not always obvious what inputs a function actually accepts.
i

Ifvwm

06/19/2019, 10:49 AM
ok, what if I don't return x.vv+y.vv, I only return x.vv that's ok?
c

cbruegg

06/19/2019, 10:49 AM
That's okay, exactly
i

Ifvwm

06/19/2019, 10:50 AM
but the editor pops up Unit was expected
c

cbruegg

06/19/2019, 10:50 AM
fun <T> justReturn(C<T> c): T { return c.vv }
You need to declare that you want to return a value of type
T
i

Ifvwm

06/19/2019, 10:50 AM
I don't understand why function should return only Unit type
c

cbruegg

06/19/2019, 10:50 AM
Using
: T
If you omit that, it defaults to
: Unit
Which is comparable to not returning anything
i

Ifvwm

06/19/2019, 10:51 AM
return ?
c

cbruegg

06/19/2019, 10:51 AM
Sorry?
i

Ifvwm

06/19/2019, 10:52 AM
how return x.vv
c

cbruegg

06/19/2019, 10:52 AM
fun <T> justReturn(C<T> c): T { return c.vv }
i

Ifvwm

06/19/2019, 10:53 AM
ok, one more thing, why I should put a <T> before the function's name
and I must define T in class or interface before I use it in function?
c

cbruegg

06/19/2019, 10:54 AM
It's a type parameter, basically you're saying "this function works for all sorts of types, not only a particular one". Like a template.
Maybe you can have a look at this article to understand generic classes and functions better: https://www.oracle.com/technetwork/articles/java/juneau-generics-2255374.html
It's for Java, but many concepts explained there apply for Kotlin as well
i

Ifvwm

06/19/2019, 10:56 AM
ok
g

gildor

06/19/2019, 4:16 PM
Have you checked guide Kotlin for Python developers by Khan Academy? It should be helpful for you https://kotlinlang.org/docs/tutorials/kotlin-for-py/introduction.html #getting-started also a good channel for such questions
i

Ifvwm

06/20/2019, 2:50 AM
ok