7
miladrt
2d

In Kotlin, I love sealed classes. They're beautiful. The compiler knows every possible subtype. When you use a when expression, it forces you to handle every single case. It's a closed, predictable, and exhaustive world. You can't have a random, unexpected state popping up at runtime.Then I look at my career. My path isn't a neat set of predefined subclasses like JuniorDev, SeniorDev, TeamLead. It's a chaotic mess of SideProjectThatBecameCritical, AccidentalSysAdmin, TemporaryScrumMaster, and ThatTimeIFixedThePrinter.The "when" expression of my professional life has a million unhandled branches and the compiler is screaming at me that there's no else block.
I just want a way to document all these weird, unexpected states so they look less like a series of runtime errors and more like a feature set.

Comments
  • 4
    It's not really a kotlin feature.

    Every static analyzer worth its salt can know the set of subclasses for a given class, despite it not being sealed.

    And Java itself already had the concept in final classes.
  • 3
    Yup, it‘s how Kotlin implements algebraic sum types.
    It’s also one of my favorite features that a programming language can have.

    You can see it as enums on steroids :)
  • 0
    I hate that it closes classes by default because the world is unpredictable and business always thinks up some new BS
  • 1
    @BordedDev well, closing is the whole point, otherwise the compiler would force you to add a default case again.

    In Swift it’s implemented as enums with associated values and there is an explicit syntax to open it for later modifications.
  • 1
    @Lensflare Yeah and I hate it, just add the damn default or opt into closed ;P

    I've had libraries I'd have to java-asm patch because they were written in kotlin and had everything closed
  • 1
    @BordedDev yeah, it really depends on your perspective.

    I know this feeling from C# projects. Often it's annoying when libraries hide or close the implementations and make customization or fixes impossible without hacks.

    I see it that way: Java is optimized for writing libs. So everything is open by default.

    C#, Kotlin and Swift are optimized for writing code that consumes libs, so everything is closed by default.

    I rarely write lib code so I appreciate the closed defaults, because it really makes the code cleaner and sometimes even more performant and secure.
Add Comment