stantronic
09/06/2022, 11:49 AMspand
09/06/2022, 11:57 AMSam
09/06/2022, 11:58 AMclass Outer {
val X = object { ... }
}
So the question isn’t so much how to do it, but why you can’t do it with the syntax
class Outer {
inner object X { ... }
}
Sam
09/06/2022, 12:05 PMstantronic
09/06/2022, 12:07 PMclass Outer {
val X = object {
val prop = "stringProp"
}
fun printIt() {
print(X.prop) // this will be a compiler error
}
}
stantronic
09/06/2022, 12:10 PMclass Outer {
inner class Inner {
val prop = "stringProp"
}
val X = Inner()
fun printIt(){
print(X.prop) // fine
}
}
spand
09/06/2022, 12:10 PMSam
09/06/2022, 12:13 PMthe anonymous object syntax behaves differently thoughthat’s a good point, it’s definitely a serious limitation that hadn’t initially occurred to me. I can see why an
inner object
would be strictly more useful than an object expression, because it would create a type as well as an instancestantronic
09/06/2022, 12:15 PMyeah, thats what i would like - makes it much more useful imo, and yet limits the creation to one per instance of the outer class.