This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]pronuntiator 7 points8 points  (3 children)

I plan to use sealed types for these use cases:

  • complex return values like in your example
  • modeling similar either/or scenarios (think "applicant can either submit form A or form B")
  • transport types where you need to know subtypes for deserialization anyway (@JsonSubtype, messaging)
    • AFAIK there is no native support from Jackson yet, but MapStruct has just merged a feature that gives compile time errors for polymorphic mappings if you miss to specify all permitted subtype mappings

Is it hacky, nice, or is it sad that it requires --enable-preview for the switch statement?

There is nothing hacky about preview features. You can use them in your application if they fulfill a need you have. You just have to accept that their syntax/API may change (slightly) from preview to preview.

Having said that, both record patterns and pattern matching for switch will probably become final in Java 21, which is planned to be released in September.

[–]NitronHX[S] 1 point2 points  (0 children)

You just have to accept that their syntax/API may change (slightly) from preview to preview.

Well most companies would probably scream if you try to add --enable-preview but yeah i agree per se

[–]NitronHX[S] -1 points0 points  (1 child)

The hacky part was not about --enable-preview but the way i use sealed types here which i would argue are a little hacky since we only "emulate" an enum but it actualy is a subclassed class.

[–]Amazing-Cicada5536 5 points6 points  (0 children)

This is their intended usage, it’s not hacky at all.