4

People at jetbrains are super awesome. They are extreme genius and doing wonders with the kotlin <3
Like it truly shows that this is a language designed specifically for their ides.
Why these thoughts ? 2 words: Sealed classes❤️

Like , if there's any other IDE used for writing kotlin code, a sealed class would not be more than a usual abstract class (afaik). But it could be also used extensively with a when(..) clause as if its an enum.
Ofcourse this behaviour is true for other IDEs. But in jetbrains IDE, you will get an added advantage of getting weak warnings when not creating the complete branches in when clause. This might have saved my code from a million random crashes
Utterly beautiful ❤️❤️

Comments
  • 1
    So confused. I'm not kotlin dev, but since I do scala I looked into it a little after hearing that kotlin is like scala, just with worse pattern matching and null handling instead of options.

    Well, isn't any class automatically sealed if you don't unseal it? And why like an abstract class? you can instantiate sealed classes, you just can't inherit from them when not in the same file. And what do you mean by them being like enums?
  • 1
    @TheCommoner282 I am not sure what is the scala meaning of sealed, but in kotlin, it means an abstract class that can only be extended and not instantiated, anywhere else.

    Why i compared it with enums was because enums also can't be instantiated. and enums are commonly used in switch cases like:

    so for enum class Gender{M,F,...}

    its usage in switch case would be like;
    switch(gender){
    case M -> {}
    case F ->{}
    ...
    }

    with sealsed classes, we can have a similar switch tree generated for us by the ide automatically based on a sealed class's children.
    sealed class equivalant of above example is:

    sealed class Gender{
    class M : Gender()
    class F :Gender()
    }

    and its usage will be:

    when(gender){
    is Gender.M -> {}
    is Gender.F ->{}
    }

    basically equating by instance type. could have been done using any other usual classes and inheritance, but ide generating branches for all the subclasses without me having to worry about the subclass location is a beautiful boon for me
  • 0
    So how many keywords does kotlin have exactly?
    It confuses the shit out of me. If there is one thing I love about go vs almost any language or there is it's minimalism. Every time I see some kotlin I'm like ok and wtf is that supposed to do.
  • 1
    @hjk101 haha , a hell lot. But most os them are not necessary as you can clearly see from this example.
    We don't need any additional sealed keyword, but when you see its usecases, you can't help but go on with it.

    I myself was very reluctant to start coding in kotlin and started this year only because of its huge list of keywords and operators. I love going back to my old rants and see how much i hated it. Basically whatever we can do in other languages , kotlin's 10% of keywords and operators could do that.
    The rest 90% of functionality that kotlin provide are like library features and dev's own best practices in other languages, but they also come out of the box for kotlin
Add Comment