News for hackers. Stuff that matters.
Re:Java 20 / JDK 20: General Availability by taftster on Tuesday March 21, 2023 @01:13PM (#35249246)
Kind of. It gets you close.

With the Builder pattern, you can easily define complex relationships for your class inner state. Like maybe if A & B are specified, then C shouldn't be specified. But if C is specified, then D should have a default value. Etc.

These types of initialization requirements are not easily duplicated with C# init-only construction. Instead, in C# you have to rely on the callers to know exactly how to correctly instantiate your class, including all the complex logic like in the example above.

So yes, C# init-only setters are kind of the best option you have. You can still create a C# FooBuilder for a Foo class. But generally C# programmers want to go out of their way to directly instantiate a Foo, and the number of bugs you get from this is crazy. It's a culture thing, honestly, not as much a language problem.

My two cents.

Re:Java 20 / JDK 20: General Availability by koyote on Tuesday March 21, 2023 @05:34PM (#35252894)
Do you have an example of how builders work in Java?
Re:Java 20 / JDK 20: General Availability by taftster on Wednesday March 22, 2023 @04:33PM (#35266698)
This is probably a pretty good link to start from.

https://blogs.oracle.com/javamagazine/post/exploring-joshua-...