oday
11/01/2022, 9:47 PMobject SomeObject
? like by default without appending any typeFrancesc
11/01/2022, 9:52 PMclass SomeObject
in JavaJacob
11/01/2022, 9:52 PMChris Lee
11/01/2022, 9:52 PMAny
is the super-type, much like Object
in Java.
e.g.Francesc
11/01/2022, 9:52 PMpublic final class SomeObject
to be exactChris Lee
11/01/2022, 9:52 PMobject SomeObject {
}
fun main(){
when(SomeObject) {
is Any -> println("Any")
else -> println("some other type")
}
}
Chris Lee
11/01/2022, 9:52 PMAny.
Chris Lee
11/01/2022, 9:53 PMThe root of the Kotlin class hierarchy. Every Kotlin class has Any as a superclass.
oday
11/01/2022, 9:53 PModay
11/01/2022, 9:53 PMJacob
11/01/2022, 9:55 PModay
11/01/2022, 9:56 PMChris Lee
11/01/2022, 9:57 PMChris Lee
11/01/2022, 9:58 PMAny
These are equivalent:
object SomeObject
object SomeObject : Any
Jacob
11/01/2022, 9:58 PMChris Lee
11/01/2022, 9:58 PModay
11/01/2022, 9:59 PMobject AnotherOne: SomeObject
oday
11/01/2022, 9:59 PMChris Lee
11/01/2022, 9:59 PModay
11/01/2022, 10:00 PMChris Lee
11/01/2022, 10:01 PMabstract class BaseClass
object SomeObject : BaseClass() {
}
object AnotherOne : BaseClass() {
}
Landry Norris
11/01/2022, 10:50 PMChris Lee
11/01/2022, 10:54 PMKlitos Kyriacou
11/02/2022, 8:56 AMit 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()
.