<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body style="overflow-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;">There is also the infamous “double curly brace” idiom:<div><br></div><div>new UIMultiSelect() {{</div><div> css(“color”, “green”);</div><div> addClass(“attention”);</div><div> maxSelection(3);</div><div>}};</div><div><br></div><div>I wonder if that could be somehow optimised…</div><div><br></div><div>Michal<br><div><br><blockquote type="cite"><div>On 13 Jun 2023, at 20:09, Attila Kelemen <attila.kelemen85@gmail.com> wrote:</div><br class="Apple-interchange-newline"><div><div dir="ltr"><div dir="ltr">Though I'm biased, because I never liked this hack, since it is just hacking the language to help with some inconveniences of the language, otherwise returning "this" is conceptually strange, since an object is returned to me which I clearly already have. Instead of having hacks like this in the language (which would then trigger a lot more questions down the line), if Java wants to address the problem, then the actual issues should be addressed. For example making it possible to build immutable objects with many fields with potential defaults.<br></div><div dir="ltr"><br></div><div>As for your case (since usually the main issue is that you want a single expression), I think it is cleaner to do this:</div><div><br></div><div>Create utility method like this:</div><div><br></div><div>```</div><div>public static <T> T with(T obj, Consumer<? super T> action) {</div><div> action.accept(obj);</div><div> return obj;</div><div>}</div><div>```</div><div><br></div><div>Which then you could just use this way (using your example):</div><div><br></div><div>```</div><div>var mySelect = with(new UIMultiSelect(), node -> {</div><div> node.css("color", "green");</div><div> node.addClass("attention");</div><div> node.maxSelection(3);</div><div>});</div><div>```</div><div><br></div><div>This also avoids the issue with chaining where people tend to nest arguments deeply (when the argument is something built this way as well), because they can't break the chain in the middle (without giving up all the benefits), so just decide to nest arguments causing stack overflow for my brain.</div><div><br></div><div>Attila</div><div><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Tomáš Bleša <<a href="mailto:blesa@anneca.cz">blesa@anneca.cz</a>> ezt írta (időpont: 2023. jún. 13., K, 19:17):<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="overflow-wrap: break-word;"><div>Hi, </div><div><br></div><div>thank you for comments and references. I didn’t mean we should create special self-type. The proposed syntax:</div><div><br></div><div><span style="font-style:normal"><font face="Courier New"> public <b>this</b> hello() {}</font></span></div><div><br></div><div>doesn’t mean “<i>returns an object of the same type as my class</i>”.</div><div><br></div><div>but rather</div><div><br></div><div>“<i>returns the same instance it is called on</i>”. The fact that it is also the same type is a useful byproduct. Please note that I used lowercase T to emphasize it is the instance not type.</div><div><br></div><div>Under the hood nothing has to be returned (on the call stack) and the method should compile to:</div><div><br></div><div><font face="Courier New"> public <b>void</b> hello() {}</font></div><div><br></div><div>That’s why using <font face="Courier New">this</font> (ThisClass/MyClass) in method arguments or other places doesn’t make sense to me. (Passing the same instance to method which already has <font face="Courier New">this</font> reference.) It also doesn’t make sense (or little) to use it on static methods like:</div><div><br></div><div><font face="Courier New"> public <b>static this</b> hello();</font></div><div><br></div><div><b>Ad Generics) </b>As you pointed out it doesn’t work well with deep class hierarchy and also reduces readability (adds boilerplate). Imagine for example big library representing UI (like DOM structure):</div><div><br></div><div><font face="Courier New">UIElement<span style="white-space:pre-wrap"> </span>// method css(…) here</font></div><div><font face="Courier New"> UILayout</font></div><div><font face="Courier New"> UIGridLayout</font></div><div><font face="Courier New"> …</font></div><div><font face="Courier New"> UIFormElement</font></div><div><font face="Courier New"> UISelect</font></div><div><font face="Courier New"> UIMultiSelect…<span style="white-space:pre-wrap"> </span>// has method maxSelection()</font></div><div><br></div><div><font face="Courier New">// compilation error</font></div><div><font face="Courier New">var mySelect = new UIMultiSelect().css(„color”, „green”)</font></div><div><font face="Courier New"><span style="white-space:pre-wrap"> </span>.addClass(„attention”).maxSelection(3);</font></div><div><br></div><div>This is basically my use-case. I have 60..100 subclasses.</div><div><br></div><div><b>Ad "Is the feature important enough?")</b></div><div><br></div><div>Technically all language features are just “a syntactic sugar” to the assembler (bytecode). I lived many years without switch-expressions, try-with-resource,… :-)</div><div><br></div><div>All the best,</div><div>Tomas</div><div><br></div></div></blockquote></div></div>
</div></blockquote></div><br></div></body></html>