https://kotlinlang.org logo
Title
c

chansek

06/12/2019, 1:56 PM
Where as this doesn't.
:thread-please: 3
d

david.bilik

06/12/2019, 1:57 PM
because the fun is declared inside your
Test
class
👆 5
if you would move your code from
main
to eg.
init
of your Test class it would work
c

chansek

06/12/2019, 2:01 PM
You made me realize that, I need to revisit my basics. Thanks a lot.
c

Czar

06/12/2019, 4:23 PM
package com.example

class Test {
   operator fun String.get(range: IntRange) = substring(range)
}

class Test2 {
   companion object {
       operator fun String.get(range: IntRange) = substring(range)
   }
}

fun main() {
   val x = "test"
   val y = with(Test()) { x[2..3] }
   val z = with(Test2) { x[2..3]}
}
😎 2
t

thana

06/13/2019, 9:12 AM
is it possible to call
get
without
with
or similar? i do understand the result of the call but i can't figure out how i would explicitly write it...
Test().get()
doesn't work.
Test()."test"[]
doesn't even make any sense...
c

Czar

06/13/2019, 9:30 AM
No,
get
here is double scoped.
t

thana

06/13/2019, 10:12 AM
ok thx