y
10/22/2024, 7:42 AMobject
that captures a local variable
that's basically a class
, right? there's no benefit to making it an object
?ephemient
10/22/2024, 7:52 AMobject
is an instance of some class (named or not), and you need an instance to captureephemient
10/22/2024, 7:53 AMfun f(name: String): Any {
return object {
override fun toString(): String = name
}
}
is roughly equivalent to
fun f(name: String): Any {
class `f$1` {
override fun toString(): String = name
}
return `f$1`()
}
y
10/22/2024, 7:54 AMobject
is just a class instance that also has the property of being a singleton?ephemient
10/22/2024, 7:54 AMobject
expression is not a singletonephemient
10/22/2024, 7:55 AMy
10/22/2024, 7:58 AMStephan Schröder
10/22/2024, 8:00 AMobject
expression is not a singleton
but doesn't create an object expression a singleton of an anonymous class??
Your second link literally says "which is useful for implementing singletons"ephemient
10/22/2024, 8:12 AMephemient
10/22/2024, 8:12 AMfun f() = object {}
f() != f()
f()::class == f()::class
Stephan Schröder
10/22/2024, 9:08 AMf()::class !== f()::class
so the classes are equivalent, but not identical. And if the classes are not identical than you could f()
still call a singleton 🤔
here is the code: https://pl.kotl.in/w35NYuhj9y
10/22/2024, 9:11 AM::class
doesn't work that way. for example,
String::class !== String::class
y
10/22/2024, 9:12 AM