not `fun foo(): Int {}`
# announcements
i
not
fun foo(): Int {}
🧵 3
d
fun 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 }
.
i
@Derek Peirce Int can be a base class, can it be a super class?
d
The extension method would work for
Int
and all subclasses of
Int
, although
Int
is not an open class and therefore cannot be subclassed.
i
@Derek Peirce
fun Int.foo(){ val c1=this@foo}
what this c1 is?
for example, 5.foo(), what c1 is in it?
d
Try running it in https://try.kotlinlang.org/ and see what happens.
s
you should probably go through the Kotlin Koans, it covers a lot of the stuff you might not be familiar with from other programming languages
👍 2
there’s also a Kotlin guide for Python programmers if that’s relevant to you
also the
this@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