<@U0CD3CDM4> what version of kotlin and IDEA you u...
# announcements
l
@gpeal what version of kotlin and IDEA you use? It works fine
j
Did you try uncommenting the line
//  println(foo().reversed())
? Does it still work?
l
yes, it works. what version of kotlin you use? in my case it 1.2.41
g
Huh, works for me, but Kotlin plugin 1.2.40 complains about this case
e
I have the issue with kotlin 1.2.41 and Intellij 2018.1
ah, it works if I wrap the example in a class and function
g
Yeah, same for me, still kinda strange that it doesn’t work for top level code (for example in kts or scratch file
e
I’ve narrowed it down further. If the
val
is a property in a class then I get the error, if it is a local variable in a function then it works
g
Yup, can confirm the same:
Copy code
open class ClassA<T> {
    open operator fun invoke(): T? = null
}

class ClassB<T : Any>(private val value: T) : ClassA<T>() {
    override operator fun invoke(): T = value
}

class OtherClass {
    private val foo: ClassA<String> = ClassB("123")

    init {
        if (foo is ClassB) {
            // This smart casts to String
            println(foo.invoke().reversed())
            // This doesn't and is still String?
            println(foo().reversed())
        }
    }
}
If I move foo inside of
init
then it fails
l
it is very interesting
Copy code
open class ClassA<T> {
    open operator fun invoke(): T? = null
}

class ClassB<T : Any>(private val value: T) : ClassA<T>() {
    override operator fun invoke(): T = value
}

class Test {
    val foo: ClassA<String> = ClassB("123")

    init {
        if (foo is ClassB) {
            // This smart casts to String
            println(foo.invoke().reversed())
            // This doesn't and is still String?
            println(foo().reversed())
            // works!
            println((foo)().reversed())
        }
    }
    @Test
    fun test() {

        if (foo is ClassB) {
            // This smart casts to String
            println(foo.invoke().reversed())
            // This doesn't and is still String?
            println(foo().reversed())
            // works!
            println((foo)().reversed())
        }
        val foo: ClassA<String> = ClassB("123")


        if (foo is ClassB) {
            // This smart casts to String
            println(foo.invoke().reversed())
            // works!
            println(foo().reversed())
            // works!
            println((foo)().reversed())
        }
    }
}
very strange
Copy code
// This doesn't and is still String?
            println(foo().reversed())
            // works!
            println((foo)().reversed())
g
I have an example in our app in which that doesn't fix it either. If I do `(foo as Success))()`then it works even though it is already in an
if (is Success)
block. Kotlin then warns about a useless cast which I have to suppress
l
you can omit cast, but do not remove brackets - this is a bug
g
@lex What do you mean?
l
@gpeal I mean that cast in
(foo as Success)()
is needless, sufficiently round brackets
(foo)()
needed for workaround bug
g
@lex that didn't work for me
l
@gpeal it works in 1.2.41