What is the purpose of empty class in Kotlin?












6















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










share|improve this question





























    6















    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










    share|improve this question



























      6












      6








      6


      2






      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










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 1 '17 at 7:42









      s1m0nw1

      29.7k654110




      29.7k654110










      asked Dec 1 '17 at 7:37









      imGsimGs

      135114




      135114
























          5 Answers
          5






          active

          oldest

          votes


















          2














          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 {
          }





          share|improve this answer


























          • Kotlin Reddit AMA ans

            – imGs
            Dec 9 '17 at 11:02



















          5














          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.






          share|improve this answer
























          • 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



















          3














          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++






          share|improve this answer

































            1














            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.






            share|improve this answer


























            • 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



















            1














            An example of empty class usage from Spring Boot framework:



            @SpringBootApplication
            class FooApplication

            fun main(args: Array<String>) {
            runApplication<FooApplication>(*args)
            }





            share|improve this answer
























              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
              });


              }
              });














              draft saved

              draft discarded


















              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









              2














              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 {
              }





              share|improve this answer


























              • Kotlin Reddit AMA ans

                – imGs
                Dec 9 '17 at 11:02
















              2














              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 {
              }





              share|improve this answer


























              • Kotlin Reddit AMA ans

                – imGs
                Dec 9 '17 at 11:02














              2












              2








              2







              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 {
              }





              share|improve this answer















              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 {
              }






              share|improve this answer














              share|improve this answer



              share|improve this answer








              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



















              • 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













              5














              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.






              share|improve this answer
























              • 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
















              5














              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.






              share|improve this answer
























              • 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














              5












              5








              5







              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.






              share|improve this answer













              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.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Dec 4 '17 at 1:50









              TomTom

              3,1622241




              3,1622241













              • 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



















              • 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

















              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











              3














              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++






              share|improve this answer






























                3














                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++






                share|improve this answer




























                  3












                  3








                  3







                  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++






                  share|improve this answer















                  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++







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Dec 1 '17 at 14:14

























                  answered Dec 1 '17 at 14:09









                  Igor WojdaIgor Wojda

                  1,0181018




                  1,0181018























                      1














                      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.






                      share|improve this answer


























                      • 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
















                      1














                      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.






                      share|improve this answer


























                      • 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














                      1












                      1








                      1







                      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.






                      share|improve this answer















                      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.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      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



















                      • 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











                      1














                      An example of empty class usage from Spring Boot framework:



                      @SpringBootApplication
                      class FooApplication

                      fun main(args: Array<String>) {
                      runApplication<FooApplication>(*args)
                      }





                      share|improve this answer




























                        1














                        An example of empty class usage from Spring Boot framework:



                        @SpringBootApplication
                        class FooApplication

                        fun main(args: Array<String>) {
                        runApplication<FooApplication>(*args)
                        }





                        share|improve this answer


























                          1












                          1








                          1







                          An example of empty class usage from Spring Boot framework:



                          @SpringBootApplication
                          class FooApplication

                          fun main(args: Array<String>) {
                          runApplication<FooApplication>(*args)
                          }





                          share|improve this answer













                          An example of empty class usage from Spring Boot framework:



                          @SpringBootApplication
                          class FooApplication

                          fun main(args: Array<String>) {
                          runApplication<FooApplication>(*args)
                          }






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jun 25 '18 at 7:43









                          mkrasowskimkrasowski

                          648




                          648






























                              draft saved

                              draft discarded




















































                              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.




                              draft saved


                              draft discarded














                              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





















































                              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







                              這個網誌中的熱門文章

                              Tangent Lines Diagram Along Smooth Curve

                              Yusuf al-Mu'taman ibn Hud

                              Zucchini