Ifvwm
05/25/2020, 3:06 AMfun foo(): Int {}
Derek Peirce
05/25/2020, 3:50 AMfun Int.foo() { ... }
is an extension function, called as 5.foo()
. fun foo(): Int { ... }
is a method that returns a value of type Int
. These are not independent, you can also have an extension function fun Int.foo(): Int { ... }
that returns an Int
, which could be, for example, fun Int.foo(): Int { return this + 5 }
.Ifvwm
05/25/2020, 3:58 AMDerek Peirce
05/25/2020, 3:59 AMInt
and all subclasses of Int
, although Int
is not an open class and therefore cannot be subclassed.Ifvwm
05/25/2020, 4:01 AMfun Int.foo(){ val c1=this@foo}
what this c1 is?Ifvwm
05/25/2020, 4:02 AMDerek Peirce
05/25/2020, 4:03 AMShawn
05/25/2020, 4:04 AMShawn
05/25/2020, 4:05 AMShawn
05/25/2020, 4:06 AMthis@foo
question is more or less answered by one of the docs links I shared earlier — you should try experimenting with examples in the docs in the Kotlin playground or in a scratch file, or even just in a main()
function in your existing project