What is the purpose of empty class in Kotlin?
I was going through Kotlin
reference document and then I saw this.
The class declaration consists of the class name, the class header
(specifying its type parameters, the primary constructor etc.) and the
class body, surrounded by curly braces. Both the header and the body
are optional; if the class has no body, curly braces can be omitted.
class Empty
Now I'm wondering what is the use of such class declaration without header and body
oop kotlin
add a comment |
I was going through Kotlin
reference document and then I saw this.
The class declaration consists of the class name, the class header
(specifying its type parameters, the primary constructor etc.) and the
class body, surrounded by curly braces. Both the header and the body
are optional; if the class has no body, curly braces can be omitted.
class Empty
Now I'm wondering what is the use of such class declaration without header and body
oop kotlin
add a comment |
I was going through Kotlin
reference document and then I saw this.
The class declaration consists of the class name, the class header
(specifying its type parameters, the primary constructor etc.) and the
class body, surrounded by curly braces. Both the header and the body
are optional; if the class has no body, curly braces can be omitted.
class Empty
Now I'm wondering what is the use of such class declaration without header and body
oop kotlin
I was going through Kotlin
reference document and then I saw this.
The class declaration consists of the class name, the class header
(specifying its type parameters, the primary constructor etc.) and the
class body, surrounded by curly braces. Both the header and the body
are optional; if the class has no body, curly braces can be omitted.
class Empty
Now I'm wondering what is the use of such class declaration without header and body
oop kotlin
oop kotlin
edited Dec 1 '17 at 7:42
s1m0nw1
29.7k654110
29.7k654110
asked Dec 1 '17 at 7:37
imGsimGs
135114
135114
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
tldr: they want to demonstrate it's possible
It's still of type Any
and therefore has certain methods automatically. I think in most cases, this does not make sense, but in the documentation case it's used to show the simplest possible definition of a class.
The Java equivalent would be:
public final class Empty {
}
Kotlin Reddit AMA ans
– imGs
Dec 9 '17 at 11:02
add a comment |
Empty classes can be useful to represent state along with other classes, especially when part of a sealed class. Eg.
sealed class MyState {
class Empty : MyState()
class Loading : MyState()
data class Content(content: String) : MyState()
data class Error(error: Throwable) : MyState()
}
In this way you can think of them like java enum entries with more flexibility.
It might be better to makeEmtpy
andLoading
anobject
instead of aclass
.
– Jesper
Dec 12 '17 at 12:05
@Jesper Probably, though sometimes there are advantages to having multiple instances of a class, even if it is empty (eg. a downstream consumer filters objects that have already been delivered and you don't want it to)
– Tom
Jan 28 at 20:22
add a comment |
From practical programmer day to day perspective empty class makes no much sense indeed. There are however cases where this behavior is desirable.
There are scenarios where we want to make sure that we want to define a class and at the same time, we want to make sure that instance of this class will never be created (type created from such class is called empty type
or uninhabited type
).
Perfect example of this is Kotlin Nothing
class with do not have class declaration header and body (notice that it also have private constructor)
https://github.com/JetBrains/kotlin/blob/master/core/builtins/native/kotlin/Nothing.kt
There are few usages for Nothing
in Kotlin language. One of them would be a function that does not return a value (do not confuse this with Unit
where the function returns actually returns a value of type Unit
). A typical example is an assertFail
method used for testing or method that exits current process. Both methods will never actually return any value yet we need to explicitly say tell it to a compiler using special type (Nothing
).
fun assertFail():Nothing {
throw Exception()
}
Nothing can be also used with start projections where type Function<*, String>
can be in-projected to Function<in Nothing, String>
Another usage for empty class is type token or placeholder:
class DatabaseColumnName
class DatabaseTableName
addItem(DatabaseColumnName.javaClass, "Age")
addItem(DatabaseTableName.javaClass, "Person")
...
getItemsByType(DatabaseTableName.javaClass)
Some languages are using empty classes for metaprogramming although I haven't explored this part personally:
Advantages of an empty class in C++
add a comment |
It doesn't make much sense as a final result. However it can be useful in active development and at a design time as a placeholder of some sort, which may be expanded in the future. Such terse syntax allows you to quickly define such new types as needed. Something like:
class Person (
val FirstName: String,
val LastName: String,
// TODO
val Address: Address
)
class Address
I think main reason this is specifically mentioned in documentation is to demonstrate, that language syntax in general can be terse, not that it is specifically created for common usage.
Can you please direct to me some kind of reference? Because just empty declaration not making any real sense to me!
– imGs
Dec 1 '17 at 7:50
add a comment |
An example of empty class usage from Spring Boot framework:
@SpringBootApplication
class FooApplication
fun main(args: Array<String>) {
runApplication<FooApplication>(*args)
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47588670%2fwhat-is-the-purpose-of-empty-class-in-kotlin%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
tldr: they want to demonstrate it's possible
It's still of type Any
and therefore has certain methods automatically. I think in most cases, this does not make sense, but in the documentation case it's used to show the simplest possible definition of a class.
The Java equivalent would be:
public final class Empty {
}
Kotlin Reddit AMA ans
– imGs
Dec 9 '17 at 11:02
add a comment |
tldr: they want to demonstrate it's possible
It's still of type Any
and therefore has certain methods automatically. I think in most cases, this does not make sense, but in the documentation case it's used to show the simplest possible definition of a class.
The Java equivalent would be:
public final class Empty {
}
Kotlin Reddit AMA ans
– imGs
Dec 9 '17 at 11:02
add a comment |
tldr: they want to demonstrate it's possible
It's still of type Any
and therefore has certain methods automatically. I think in most cases, this does not make sense, but in the documentation case it's used to show the simplest possible definition of a class.
The Java equivalent would be:
public final class Empty {
}
tldr: they want to demonstrate it's possible
It's still of type Any
and therefore has certain methods automatically. I think in most cases, this does not make sense, but in the documentation case it's used to show the simplest possible definition of a class.
The Java equivalent would be:
public final class Empty {
}
edited Nov 23 '18 at 12:23
answered Dec 1 '17 at 7:41
s1m0nw1s1m0nw1
29.7k654110
29.7k654110
Kotlin Reddit AMA ans
– imGs
Dec 9 '17 at 11:02
add a comment |
Kotlin Reddit AMA ans
– imGs
Dec 9 '17 at 11:02
Kotlin Reddit AMA ans
– imGs
Dec 9 '17 at 11:02
Kotlin Reddit AMA ans
– imGs
Dec 9 '17 at 11:02
add a comment |
Empty classes can be useful to represent state along with other classes, especially when part of a sealed class. Eg.
sealed class MyState {
class Empty : MyState()
class Loading : MyState()
data class Content(content: String) : MyState()
data class Error(error: Throwable) : MyState()
}
In this way you can think of them like java enum entries with more flexibility.
It might be better to makeEmtpy
andLoading
anobject
instead of aclass
.
– Jesper
Dec 12 '17 at 12:05
@Jesper Probably, though sometimes there are advantages to having multiple instances of a class, even if it is empty (eg. a downstream consumer filters objects that have already been delivered and you don't want it to)
– Tom
Jan 28 at 20:22
add a comment |
Empty classes can be useful to represent state along with other classes, especially when part of a sealed class. Eg.
sealed class MyState {
class Empty : MyState()
class Loading : MyState()
data class Content(content: String) : MyState()
data class Error(error: Throwable) : MyState()
}
In this way you can think of them like java enum entries with more flexibility.
It might be better to makeEmtpy
andLoading
anobject
instead of aclass
.
– Jesper
Dec 12 '17 at 12:05
@Jesper Probably, though sometimes there are advantages to having multiple instances of a class, even if it is empty (eg. a downstream consumer filters objects that have already been delivered and you don't want it to)
– Tom
Jan 28 at 20:22
add a comment |
Empty classes can be useful to represent state along with other classes, especially when part of a sealed class. Eg.
sealed class MyState {
class Empty : MyState()
class Loading : MyState()
data class Content(content: String) : MyState()
data class Error(error: Throwable) : MyState()
}
In this way you can think of them like java enum entries with more flexibility.
Empty classes can be useful to represent state along with other classes, especially when part of a sealed class. Eg.
sealed class MyState {
class Empty : MyState()
class Loading : MyState()
data class Content(content: String) : MyState()
data class Error(error: Throwable) : MyState()
}
In this way you can think of them like java enum entries with more flexibility.
answered Dec 4 '17 at 1:50
TomTom
3,1622241
3,1622241
It might be better to makeEmtpy
andLoading
anobject
instead of aclass
.
– Jesper
Dec 12 '17 at 12:05
@Jesper Probably, though sometimes there are advantages to having multiple instances of a class, even if it is empty (eg. a downstream consumer filters objects that have already been delivered and you don't want it to)
– Tom
Jan 28 at 20:22
add a comment |
It might be better to makeEmtpy
andLoading
anobject
instead of aclass
.
– Jesper
Dec 12 '17 at 12:05
@Jesper Probably, though sometimes there are advantages to having multiple instances of a class, even if it is empty (eg. a downstream consumer filters objects that have already been delivered and you don't want it to)
– Tom
Jan 28 at 20:22
It might be better to make
Emtpy
and Loading
an object
instead of a class
.– Jesper
Dec 12 '17 at 12:05
It might be better to make
Emtpy
and Loading
an object
instead of a class
.– Jesper
Dec 12 '17 at 12:05
@Jesper Probably, though sometimes there are advantages to having multiple instances of a class, even if it is empty (eg. a downstream consumer filters objects that have already been delivered and you don't want it to)
– Tom
Jan 28 at 20:22
@Jesper Probably, though sometimes there are advantages to having multiple instances of a class, even if it is empty (eg. a downstream consumer filters objects that have already been delivered and you don't want it to)
– Tom
Jan 28 at 20:22
add a comment |
From practical programmer day to day perspective empty class makes no much sense indeed. There are however cases where this behavior is desirable.
There are scenarios where we want to make sure that we want to define a class and at the same time, we want to make sure that instance of this class will never be created (type created from such class is called empty type
or uninhabited type
).
Perfect example of this is Kotlin Nothing
class with do not have class declaration header and body (notice that it also have private constructor)
https://github.com/JetBrains/kotlin/blob/master/core/builtins/native/kotlin/Nothing.kt
There are few usages for Nothing
in Kotlin language. One of them would be a function that does not return a value (do not confuse this with Unit
where the function returns actually returns a value of type Unit
). A typical example is an assertFail
method used for testing or method that exits current process. Both methods will never actually return any value yet we need to explicitly say tell it to a compiler using special type (Nothing
).
fun assertFail():Nothing {
throw Exception()
}
Nothing can be also used with start projections where type Function<*, String>
can be in-projected to Function<in Nothing, String>
Another usage for empty class is type token or placeholder:
class DatabaseColumnName
class DatabaseTableName
addItem(DatabaseColumnName.javaClass, "Age")
addItem(DatabaseTableName.javaClass, "Person")
...
getItemsByType(DatabaseTableName.javaClass)
Some languages are using empty classes for metaprogramming although I haven't explored this part personally:
Advantages of an empty class in C++
add a comment |
From practical programmer day to day perspective empty class makes no much sense indeed. There are however cases where this behavior is desirable.
There are scenarios where we want to make sure that we want to define a class and at the same time, we want to make sure that instance of this class will never be created (type created from such class is called empty type
or uninhabited type
).
Perfect example of this is Kotlin Nothing
class with do not have class declaration header and body (notice that it also have private constructor)
https://github.com/JetBrains/kotlin/blob/master/core/builtins/native/kotlin/Nothing.kt
There are few usages for Nothing
in Kotlin language. One of them would be a function that does not return a value (do not confuse this with Unit
where the function returns actually returns a value of type Unit
). A typical example is an assertFail
method used for testing or method that exits current process. Both methods will never actually return any value yet we need to explicitly say tell it to a compiler using special type (Nothing
).
fun assertFail():Nothing {
throw Exception()
}
Nothing can be also used with start projections where type Function<*, String>
can be in-projected to Function<in Nothing, String>
Another usage for empty class is type token or placeholder:
class DatabaseColumnName
class DatabaseTableName
addItem(DatabaseColumnName.javaClass, "Age")
addItem(DatabaseTableName.javaClass, "Person")
...
getItemsByType(DatabaseTableName.javaClass)
Some languages are using empty classes for metaprogramming although I haven't explored this part personally:
Advantages of an empty class in C++
add a comment |
From practical programmer day to day perspective empty class makes no much sense indeed. There are however cases where this behavior is desirable.
There are scenarios where we want to make sure that we want to define a class and at the same time, we want to make sure that instance of this class will never be created (type created from such class is called empty type
or uninhabited type
).
Perfect example of this is Kotlin Nothing
class with do not have class declaration header and body (notice that it also have private constructor)
https://github.com/JetBrains/kotlin/blob/master/core/builtins/native/kotlin/Nothing.kt
There are few usages for Nothing
in Kotlin language. One of them would be a function that does not return a value (do not confuse this with Unit
where the function returns actually returns a value of type Unit
). A typical example is an assertFail
method used for testing or method that exits current process. Both methods will never actually return any value yet we need to explicitly say tell it to a compiler using special type (Nothing
).
fun assertFail():Nothing {
throw Exception()
}
Nothing can be also used with start projections where type Function<*, String>
can be in-projected to Function<in Nothing, String>
Another usage for empty class is type token or placeholder:
class DatabaseColumnName
class DatabaseTableName
addItem(DatabaseColumnName.javaClass, "Age")
addItem(DatabaseTableName.javaClass, "Person")
...
getItemsByType(DatabaseTableName.javaClass)
Some languages are using empty classes for metaprogramming although I haven't explored this part personally:
Advantages of an empty class in C++
From practical programmer day to day perspective empty class makes no much sense indeed. There are however cases where this behavior is desirable.
There are scenarios where we want to make sure that we want to define a class and at the same time, we want to make sure that instance of this class will never be created (type created from such class is called empty type
or uninhabited type
).
Perfect example of this is Kotlin Nothing
class with do not have class declaration header and body (notice that it also have private constructor)
https://github.com/JetBrains/kotlin/blob/master/core/builtins/native/kotlin/Nothing.kt
There are few usages for Nothing
in Kotlin language. One of them would be a function that does not return a value (do not confuse this with Unit
where the function returns actually returns a value of type Unit
). A typical example is an assertFail
method used for testing or method that exits current process. Both methods will never actually return any value yet we need to explicitly say tell it to a compiler using special type (Nothing
).
fun assertFail():Nothing {
throw Exception()
}
Nothing can be also used with start projections where type Function<*, String>
can be in-projected to Function<in Nothing, String>
Another usage for empty class is type token or placeholder:
class DatabaseColumnName
class DatabaseTableName
addItem(DatabaseColumnName.javaClass, "Age")
addItem(DatabaseTableName.javaClass, "Person")
...
getItemsByType(DatabaseTableName.javaClass)
Some languages are using empty classes for metaprogramming although I haven't explored this part personally:
Advantages of an empty class in C++
edited Dec 1 '17 at 14:14
answered Dec 1 '17 at 14:09
Igor WojdaIgor Wojda
1,0181018
1,0181018
add a comment |
add a comment |
It doesn't make much sense as a final result. However it can be useful in active development and at a design time as a placeholder of some sort, which may be expanded in the future. Such terse syntax allows you to quickly define such new types as needed. Something like:
class Person (
val FirstName: String,
val LastName: String,
// TODO
val Address: Address
)
class Address
I think main reason this is specifically mentioned in documentation is to demonstrate, that language syntax in general can be terse, not that it is specifically created for common usage.
Can you please direct to me some kind of reference? Because just empty declaration not making any real sense to me!
– imGs
Dec 1 '17 at 7:50
add a comment |
It doesn't make much sense as a final result. However it can be useful in active development and at a design time as a placeholder of some sort, which may be expanded in the future. Such terse syntax allows you to quickly define such new types as needed. Something like:
class Person (
val FirstName: String,
val LastName: String,
// TODO
val Address: Address
)
class Address
I think main reason this is specifically mentioned in documentation is to demonstrate, that language syntax in general can be terse, not that it is specifically created for common usage.
Can you please direct to me some kind of reference? Because just empty declaration not making any real sense to me!
– imGs
Dec 1 '17 at 7:50
add a comment |
It doesn't make much sense as a final result. However it can be useful in active development and at a design time as a placeholder of some sort, which may be expanded in the future. Such terse syntax allows you to quickly define such new types as needed. Something like:
class Person (
val FirstName: String,
val LastName: String,
// TODO
val Address: Address
)
class Address
I think main reason this is specifically mentioned in documentation is to demonstrate, that language syntax in general can be terse, not that it is specifically created for common usage.
It doesn't make much sense as a final result. However it can be useful in active development and at a design time as a placeholder of some sort, which may be expanded in the future. Such terse syntax allows you to quickly define such new types as needed. Something like:
class Person (
val FirstName: String,
val LastName: String,
// TODO
val Address: Address
)
class Address
I think main reason this is specifically mentioned in documentation is to demonstrate, that language syntax in general can be terse, not that it is specifically created for common usage.
edited Dec 1 '17 at 8:00
answered Dec 1 '17 at 7:45
Petr AbdulinPetr Abdulin
23.8k64783
23.8k64783
Can you please direct to me some kind of reference? Because just empty declaration not making any real sense to me!
– imGs
Dec 1 '17 at 7:50
add a comment |
Can you please direct to me some kind of reference? Because just empty declaration not making any real sense to me!
– imGs
Dec 1 '17 at 7:50
Can you please direct to me some kind of reference? Because just empty declaration not making any real sense to me!
– imGs
Dec 1 '17 at 7:50
Can you please direct to me some kind of reference? Because just empty declaration not making any real sense to me!
– imGs
Dec 1 '17 at 7:50
add a comment |
An example of empty class usage from Spring Boot framework:
@SpringBootApplication
class FooApplication
fun main(args: Array<String>) {
runApplication<FooApplication>(*args)
}
add a comment |
An example of empty class usage from Spring Boot framework:
@SpringBootApplication
class FooApplication
fun main(args: Array<String>) {
runApplication<FooApplication>(*args)
}
add a comment |
An example of empty class usage from Spring Boot framework:
@SpringBootApplication
class FooApplication
fun main(args: Array<String>) {
runApplication<FooApplication>(*args)
}
An example of empty class usage from Spring Boot framework:
@SpringBootApplication
class FooApplication
fun main(args: Array<String>) {
runApplication<FooApplication>(*args)
}
answered Jun 25 '18 at 7:43
mkrasowskimkrasowski
648
648
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47588670%2fwhat-is-the-purpose-of-empty-class-in-kotlin%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown