ElawsDev
03/07/2024, 2:47 PMElawsDev
03/07/2024, 2:47 PMsealed interface A{
class B(val value: String): A
class C : A
object D : A
}
Instanciated in :
var a : A
1. I thought interface was something a class could implement. How come classes nested in the body of A
can implement A
itself ? Isn't it an infinite loop (since B
implements A
, so it should contain B
itself, in addition to C
and D
) ?
2. How come we can instantiate an interface (var a : A
) ? I never heard such thing was possible ? Interfaces are defined here in the Kotlin documentation, and "interface instantiation" is not mentioned a single time.
3. Why use object D
instead of class D
? What is the difference ?
Would love if someone could provide some simple explanation of the concepts at play here.
Thank you very much.ephemient
03/07/2024, 2:51 PMephemient
03/07/2024, 2:52 PMclass A {
companion object Companion
}
A.Companion
is a class nested inside A
(also it is a singleton object, and that is the default name of companion objects so it doesn't need to be written out). that does not mean that A.Companion
is an instance of A
Sam
03/07/2024, 2:53 PMB
inside the body of the interface A
doesn't make B
a member of A
.
2. You can't. a = A
does not do anything and won't compile.
3. Objects are useful if you know you're only ever going to need one instance of your new type, which is often the case if the type doesn't have any state of its own.ephemient
03/07/2024, 2:53 PMopen class A
class B : A()
are not nestedephemient
03/07/2024, 2:54 PMsealed
keyword doesn't change that,
sealed interface A {
class B : A
class C
}
class D : A
in this case, A.B
and D
implement `A`; A.C
does not. it has nothing to do with nestingElawsDev
03/07/2024, 3:04 PMvar a: A
(OP fixed) : how such thing is possible ? We can declare a variable of the type of an interface ? I thought we could only declare variables of the type of a class that implements an interface ?
5. So if I have the following :
sealed interface A{
var myVar: String = "" // typo, can't initialize like this in interface.
class B(val value: String): A
class C : A
object D : A
}
Then B
, C
and D
will have myVar
property as well, as in the case of usual interface, since they all implement A
?Sam
03/07/2024, 3:07 PMList
is an interface in Kotlin. When you write val x = listOf(1, 2, 3)
, the type of x
is `ListInt`—you don't need to know what actual class of list Kotlin has chosen to create under the hood.
5. Yes ✅ (almost)ephemient
03/07/2024, 3:07 PMinterface
ephemient
03/07/2024, 3:09 PMclass A { var myVar: String = "" } // OK
interface A { var myVar: String = "" } // OK
abstract class A { abstract var myVar: String } // OK
interface A { var myVar: String = "" } // not OK
again, not related to sealed
, it's just the fact that a non-abstract var
needs storage, an initializer needs to be non-abstract, and an interface
cannot have concrete membersElawsDev
03/07/2024, 3:16 PMvar myVar: String
instead of var myVar: String = ""
because the latter is not possible in an interface indeed, the property has to be abstract or implement an accessor. Otherwise, the reasoning was correct for point n°5 ? (also, there seem to be a typo in your code example ?)