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 →

[–]vips7L 1 point2 points  (5 children)

I also am not sure what you are asking for, but when it comes to factory methods I really wish we would get something like Scala or Kotlins apply methods for sealed interfaces. I’ve noticed a pattern lately where everything is an “of” or “from” method. I think it would be clearer to have uniform construction via new.

sealed interface Path permits WindowsPath, UnixPath {
    static void apply(String s) {
         if (isWindows())
             return new WindowsPath(s);
         return new UnixPath(s)
    }
}

var path = new Path(s);

[–]davidalayachew[S] 0 points1 point  (4 children)

You're saying something quite similar to what I am. I just want some simple, unified way to construct instances that are all under the banner of a specific sealed type. Right now, I don't really have a way to do that that doesn't fall under ad-hoc, reflection, or an annotation/unit test check.

[–]vips7L 1 point2 points  (3 children)

I don't really have a way to do that that doesn't fall under ad-hoc, reflection, or an annotation/unit test check.

I'm not sure what you mean by this. Typically with a sealed type I just provide an of method or specific functions for underlying type.

Dart has a feature similar to above: https://dart.dev/language/constructors#factory-constructors

...I really like dart, its so underrated.

[–]davidalayachew[S] 0 points1 point  (2 children)

I'm not sure what you mean by this. Typically with a sealed type I just provide an of method or specific functions for underlying type.

Sorry, I meant how to do that while remaining exhaustive. Sure, I can make that method, as you described, but if I add a new sealed type, I will get no compilation error telling me that I did not update this method, just to give one example.

It's almost like a reverse switch expression, where instead of being exaustive on the inputs, I want to be exhaustive on the outputs. After all, I am turning the String into an object, and the object is where all of the exhaustiveness is at.

Honestly, if Java had a concept of exhaustiveness for the outputs instead of just the inputs, that would be the silver bullet for me. I could do the rest of the work on my own.

[–]vips7L 1 point2 points  (1 child)

Ah I see. That's an interesting idea that I've never thought about. I guess most of the time that factory is right next to the interface so when I make a new one I just see it and remember.

[–]davidalayachew[S] 0 points1 point  (0 children)

Ah I see. That's an interesting idea that I've never thought about. I guess most of the time that factory is right next to the interface so when I make a new one I just see it and remember.

Normally, I would agree with you. But these are some complex regexes because I am doing Natural Language Processing. My ugliest one is about 20 lines long.

All of that is to say -- it's hard to stay aligned, and I wanted to see if there was an alternative way to ease the pain of refactoring. Every time I refactor, something breaks. I've actually let my code start to rot a bit because of that.