Hi guys, I'm a newcomer to kotlin and I've been wo...
# getting-started
v
Hi guys, I'm a newcomer to kotlin and I've been working through the Kotlin in Action book. I'm currently on chapter 6.3.5 and I don't quite get this snippet:
Copy code
>>> val letters = Array(26) { i -> ('a' + i).toString() }
>>> println(letters.joinToString(""))
abcdefghijklmnopqrstuvwxyz
What exactly is
'a' + i
? I've done a little bit of testing and depending on
i
it will return a character. For example:
Copy code
println('a' + 2) 
c
It seems very basic but I can't find any more detail explanation for this.
r
It means
'a'.plus(2)
, ie calling into this function: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char/plus.html
you’ll read more about operator functions later in the book
v
Yeah I found the
operator
function in Char.kt. It a little bit confusing at first since there's no implementation detail.
Copy code
/** Adds the other Int value to this value resulting a Char. */
    public operator fun plus(other: Int): Char
r
yeah I guess it’s worded a little weirdly, but it’s doing essentially what you’d expect. It takes the index associated with the
char
(ie the ascii value), adds the other
int
to it, and returns the
char
associated with that index.
😀 1