spierce7
05/12/2020, 6:12 PMobject
? That seems to be a misnomer to me, and that it should be class
instead. What am I missing?Zach Klippenstein (he/him) [MOD]
05/12/2020, 6:15 PMspierce7
05/12/2020, 6:33 PMZach Klippenstein (he/him) [MOD]
05/12/2020, 6:37 PMobject
creates an instance. If that object is top-level, then it’s a singleton.streetsofboston
05/12/2020, 6:42 PMobject
does not always imply a singleton.
fun test() {
val callback = object : Callback<Int> {
override fun onSuccess(value: Int) { ... }
}
...
}
spierce7
05/12/2020, 6:45 PMobject
a misnomer there?streetsofboston
05/12/2020, 6:48 PMcallback
variable has an anonymous type, whose base-class is Callback<Int>
and this type’s one and only value is held by callback
Joel
05/12/2020, 6:59 PMshikasd
05/12/2020, 7:07 PMobject
feels more like an instance
also you can create an anonymous object without extending anything
val a = object {
val b: Int = 0
}
println(a.b)
In that regard, named object
feels like just a handy way of doing the above for top level without creating variables.Casey Brooks
05/12/2020, 7:23 PMalex j
05/12/2020, 7:43 PMval a = object {
val b: Int = 0
}
println(a.b)
for this example, is the object
keyword required? why not
val a = {
val b: Int = 0
}
println(a.b)
Joel
05/12/2020, 7:46 PMa
is assigned to an anonymous function where b
is a local variable, which is entirely different than the first example.alex j
05/12/2020, 7:49 PMprintln(a.b)
even a legal statement?Joel
05/12/2020, 7:49 PM