what is the type of this object `object SomeObject...
# getting-started
o
what is the type of this object
object SomeObject
? like by default without appending any type
f
same as
class SomeObject
in Java
j
objects are singletons so I guess SomeObject is the type?
c
In Kotlin
Any
is the super-type, much like
Object
in Java. e.g.
f
public final class SomeObject
to be exact
c
Copy code
object SomeObject {
    
}

fun main(){
    when(SomeObject) {
        is Any -> println("Any")
        else -> println("some other type")
    }

}
…prints
Any.
💯 1
The root of the Kotlin class hierarchy. Every Kotlin class has Any as a superclass.
o
right right, sorry i could’ve just tried that, yes makes sense that it’s Any
thank you
👍 1
j
everything is Any. I could say Int is Any. I'm not sure if Any is the answer you're looking for
4
o
it is, cause the next topic I’m reading about was that “you could give objects a type”, and I thought “oh, but what if I didn’t, what type would it be”
c
in your example (an object) you are defining a type and creating a singleton instance of it.
☝️ 1
in the case of not declaring a super-type, the default is
Any
These are equivalent:
Copy code
object SomeObject
object SomeObject : Any
j
c
of course SomeObject is an instance of SomeObject.
o
but you cannot say
object AnotherOne: SomeObject
cannot inherit from Singleton
c
that’s correct.
o
aha, so it is a type, you can have a list of SomeObject, ok
c
correct. If you want to mix in inheritance you can define a super-class, or implement an interface.
Copy code
abstract class BaseClass
object SomeObject : BaseClass() {
    
}
object AnotherOne : BaseClass() {
    
}
l
Regarding the when statement, in Kotlin, when statements are short-circuiting. Everything is an instance of Any, so when that branch evaluates to true, it stops checking the others. Running println(SomeObject is SomeObject) prints true, as would is Any.
c
that’s correct. the checks are run in order. the goal was to demonstrate that SomeObject is an instance of Any.
k
it is, cause the next topic I’m reading about was that “you could give objects a type”, and I thought “oh, but what if I didn’t, what type would it be”
The statement “you could give objects a type” can be misleading. If by “objects” they mean instances of things declared using the
object
keyword, then it's misleading because all such objects already have a type. You can give them a super class type but not an actual object type. On the other hand, if by “objects” they mean instances of a class, then of course you could give them a type as in
val myObject: MyClass = foo()
.