Apart from the obvious syntactic difference, are t...
# getting-started
c
Apart from the obvious syntactic difference, are there any other differences between these two snippets, performance-wise, etc?
Copy code
// #1

class Outer {
    fun inner(): Any = Inner()

    private inner class Inner
}
Copy code
// #2

class Outer {
    fun inner(): Any = Inner(this)

    private class Inner(val outer: Outer)
}
From my understanding, these are both absolutely identical, but maybe some platforms have magic ways of handling the first one better?
h
You're not allowed to nest a non-inner class in an inner class. I don't know the reason, but maybe it's related to some kind of fundamental difference between the two snippets. Or maybe this kind of nesting is just a bad idea, and no effort was made to allow it.
Copy code
class Outer {
    fun inner(): Any = Inner()

    private inner class Inner {
        class A // compile error: must be inner class
    }
}
Copy code
class Outer {
    fun inner(): Any = Inner(this)

    private class Inner(val outer: Outer) {
        class A // allowed
    }
}
c
Uh, that's an interesting difference. Thanks for the info
e
it seems more explicable with
object
,
Copy code
class Outer {
    inner class Inner {
        companion object
(or any other nested
object
type) is rejected, while
Copy code
class Outer {
    class Inner {
        companion object
is OK. which sort of makes sense to me as
Outer.Inner
can't exist without an
Outer
, so an
Outer.Inner.Companion
which can exist without
Outer
is strange
I don't see anything in https://kotlinlang.org/spec/ that looks like it mentions this though