https://kotlinlang.org logo
#getting-started
Title
# getting-started
e

ElawsDev

03/07/2024, 2:47 PM
A few questions about sealed interface syntax
As a beginner, it'll probably be a while before I use that, but just out of curiosity : E.g. :
Copy code
sealed interface A{
    class B(val value: String): A
    class C : A
    object D : A
}
Instanciated in :
Copy code
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.
e

ephemient

03/07/2024, 2:51 PM
nesting is different than inheritance
for example, in the more common case of
Copy code
class 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
1
s

Sam

03/07/2024, 2:53 PM
1. Putting the class
B
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.
e

ephemient

03/07/2024, 2:53 PM
conversely, the most common cases of inheritance
Copy code
open class A
class B : A()
are not nested
the
sealed
keyword doesn't change that,
Copy code
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 nesting
e

ElawsDev

03/07/2024, 3:04 PM
Thank you @Sam and @ephemient, it's a bit clearer. 4. Sorry, for point n°2, I meant
var 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 :
Copy code
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
?
s

Sam

03/07/2024, 3:07 PM
4. Using an interface as a type is exactly what interfaces are for. As an example,
List
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)
👍 1
e

ephemient

03/07/2024, 3:07 PM
you actually can't quite do that with an
interface
blob think smart 1
Copy code
class 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 members
e

ElawsDev

03/07/2024, 3:16 PM
@ephemient: Indeed, I meant
var 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 ?)
👍 1