<html><body><div style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: #000000"><div><br></div><div><br></div><hr id="zwchr" data-marker="__DIVIDER__"><div data-marker="__HEADERS__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><b>From: </b>"Vikram Bakshi" <vab2048@gmail.com><br><b>To: </b>"Remi Forax" <forax@univ-mlv.fr><br><b>Cc: </b>"amber-dev" <amber-dev@openjdk.java.net><br><b>Sent: </b>Friday, January 5, 2024 12:13:17 PM<br><b>Subject: </b>Re: Allowing inheritance with Records?<br></blockquote></div><div data-marker="__QUOTED_TEXT__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><div dir="ltr"><br><div>> I almost spill my tea :)
</div><br><div>:)</div><br><div>
<div>> Inheritance is about sharing behaviors, so the data behaviors
should not change too much. But business requirement, business
data/computation changes are frequent, so using inheritance is a kind of
an an anti-pattern for data.</div><br>
</div><div>Is it really an anti-pattern in all situations though? There have been times where I have wanted my record to inherit from a record representing some "id" which itself contains methods/behaviour. I can think of it being useful in some situations.</div></div></blockquote><div><br></div><div>I will never pretend that I know all situations :)<br></div><div><br data-mce-bogus="1"></div><div>Also with an interface + default methods, you also get most the features of inheritance without using inheritance<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>interface Identified {<br data-mce-bogus="1"></div><div> int id(); // abstract<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div> default void behavior() {<br data-mce-bogus="1"></div><div> System.our.println(id());<br data-mce-bogus="1"></div><div> }<br data-mce-bogus="1"></div><div>}<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>record Foo(int id) implements Identified { }<br data-mce-bogus="1"></div><div>record Bar(int id) implements Identified { }<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>And as I said it can also written like this<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div><div>interface Identified { } // empty interface<br></div><div><div>record Foo(int id) implements Identified { }</div><div>record Bar(int id) implements Identified { }</div><div><br data-mce-bogus="1"></div><div>void behavior(Identified identified) {<br data-mce-bogus="1"></div><div> System.out.println(switch(identified) {</div><div> case Foo(var id) -> id;<br> case Bar(var id) -> id;<br data-mce-bogus="1"></div><div> });<br data-mce-bogus="1"></div><div>}<br data-mce-bogus="1"></div></div></div><div><br data-mce-bogus="1"></div><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><div dir="ltr"><div><br></div><br><div>
<div>> It's far far easier to use records, empty interfaces and pattern
matching (switch on types) when you want to define data and their
business rules than tryig to use inheritance for a case it will not work
well.</div>
</div><br><div>I think allowing the developer the flexibility to choose whether or not they would like inheritance would still be a valid "Java" way of doing things. <br></div><br><div>I understand from Brian's email that record inheritance is currently shelved so we will have to wait and see if it ever becomes a priority (other upcoming features like the "state monad" for records are probably more important anyway <a href="https://github.com/openjdk/amber-docs/blob/master/eg-drafts/reconstruction-records-and-classes.md" target="_blank">https://github.com/openjdk/amber-docs/blob/master/eg-drafts/reconstruction-records-and-classes.md</a>). </div></div></blockquote><div><br></div><div>There is a technical reson to not allow inheritance of record.<br data-mce-bogus="1"></div><div>Components of records are trusted by the JIT, i.e. if the JIT see the value of a record component, instead of reading the value again from the instance, it can use the value directly<br data-mce-bogus="1"></div><div>(the values captured by a lambda works the same way BTW).<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>Now enter inheritance, the constructor of the base class is called before the fields are initialized, so if a component is observed by the VM at that point, the default value (null, 0, false, etc) will be seen because the component is not yet initialized. And then the JIT may replace an access to the component by the default value instead of the proper value.</div><div><br data-mce-bogus="1"></div><div>As part of Valhalla, we are working to force the constructor of the base class to be called *after* the field values are initialized.<br></div><div><br data-mce-bogus="1"></div><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><div dir="ltr"><div>Regards,</div><div>Vikram<br></div><div> <br></div></div></blockquote><div><br></div><div>regards,<br data-mce-bogus="1"></div><div>Rémi<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Dec 29, 2023 at 2:05 PM Remi Forax <<a href="mailto:forax@univ-mlv.fr" target="_blank">forax@univ-mlv.fr</a>> wrote:<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><div style="font-family:arial,helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)"><br><br><hr id="m_6840048097803779319zwchr"><div><blockquote style="border-left:2px solid rgb(16,16,255);margin-left:5px;padding-left:5px;color:rgb(0,0,0);font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt"><b>From: </b>"Vikram Bakshi" <<a href="mailto:vab2048@gmail.com" target="_blank">vab2048@gmail.com</a>><br><b>To: </b><a href="mailto:amber-dev@openjdk.java.net" target="_blank">amber-dev@openjdk.java.net</a><br><b>Sent: </b>Thursday, December 28, 2023 3:59:10 PM<br><b>Subject: </b>Allowing inheritance with Records?<br></blockquote></div><div><blockquote style="border-left:2px solid rgb(16,16,255);margin-left:5px;padding-left:5px;color:rgb(0,0,0);font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt"><div dir="ltr"><div>Hello,</div><br><div>Is the decision to not allow inheritance for records set in stone? Or will this be opened up and explored in the future?</div><br><div>One of the goals of records (from Brian Goetz Devoxx talk) is to "model data as data", and allowing inheritance would offer a powerful way of modelling data. </div></div></blockquote><br><div>I almost spill my tea :)<br></div><div>Inheritance is about sharing behaviors, so the data behaviors should not change too much. But business requirement, business data/computation changes are frequent, so using inheritance is a kind of an an anti-pattern for data.</div><br><div>It's far far easier to use records, empty interfaces and pattern matching (switch on types) when you want to define data and their business rules than tryig to use inheritance for a case it will not work well.</div><br><blockquote style="border-left:2px solid rgb(16,16,255);margin-left:5px;padding-left:5px;color:rgb(0,0,0);font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt"><div dir="ltr"><br><br><div>Right now we have to write interfaces for fields which are shared/static methods for shared behaviour/copying and pasting - which I do not think is ideal.</div><br><div>Regards,</div><div>Vikram<br></div></div></blockquote><br><div>regards,<br></div><div>Rémi</div></div></div></div></blockquote></div><br></blockquote></div></div></body></html>