From brian.goetz at oracle.com Wed Jun 14 14:57:44 2017 From: brian.goetz at oracle.com (Brian Goetz) Date: Wed, 14 Jun 2017 10:57:44 -0400 Subject: Pattern matching -- translation Message-ID: <808e95d0-c964-afcf-dd15-b89384283f30@oracle.com> I've posted a writeup on a strategy for pattern match translation here: http://cr.openjdk.java.net/~briangoetz/amber/pattern-match-translation.html Comments welcome! From brian.goetz at oracle.com Wed Jun 14 15:32:53 2017 From: brian.goetz at oracle.com (Brian Goetz) Date: Wed, 14 Jun 2017 11:32:53 -0400 Subject: Pattern matching -- translation In-Reply-To: <55A927AD-318F-47EE-83AA-4A69649DCC21@d-d.me> References: <808e95d0-c964-afcf-dd15-b89384283f30@oracle.com> <55A927AD-318F-47EE-83AA-4A69649DCC21@d-d.me> Message-ID: On 6/14/2017 11:21 AM, Dmitry Petrashko wrote: > > Hello Brian, > > It would be very nice to see if the technology you are developing can > simplify pattern matching in Scala and\or help us > expose out pattern matching capabilities to other JVM languages. > Absolutely. Bridging an existing unapply method to a __Pattern should be trivial, meaning that existing Scala classfiles, without recompilation, still have a path to playing. For newly compiled classfiles, you further have the option of generating __Pattern factories as well. So there should be a clean story for both old code and new. > Scala has an additional specced requirement to the patterns: > |unapply|-s should be pure, not mutate any environment of the objects > that they are given. This means that we can potentially remove calls > to them if we know that they won?t match and not worry about removing > side-effects. It would be nice to know our opinion of if you?d like to > have a similar limitation on your |predicate|s. > While we can't enforce this, we can tell people that they get no guarantees about the ordering or execution arity of side effects. So, yes, basically the same deal, just in different language. > On a minor note your Scala examples are slightly outdated. We > currently have a scheme that commonly does not allocate any new objects: > Can you post a more detailed translation scheme for the non-case-class case? > The most important part is that case classes in Scala automatically > synthesise those methods on the class instance. > This allows for |unapply| to be an identity method, that does not > require any allocations! No options or topples get allocated. > This sounds like it has a similar effect to the optimization where we use an object as its own carrier, at least for case classes? From maurizio.cimadamore at oracle.com Thu Jun 15 11:58:37 2017 From: maurizio.cimadamore at oracle.com (Maurizio Cimadamore) Date: Thu, 15 Jun 2017 12:58:37 +0100 Subject: [enhanced enums] - end of the road? In-Reply-To: <8cdd82b6-50c2-1913-44ac-560ea78ea6b3@gmail.com> References: <786de6e1-8ca4-48ab-24b4-57460c232cef@oracle.com> <8cdd82b6-50c2-1913-44ac-560ea78ea6b3@gmail.com> Message-ID: <4c94d69e-aa91-e8f2-1460-a67a318ef253@oracle.com> I think the moral equivalent of what you wanted to say is something like this: enum Option implements Consumer { D implements Generic("-d", ...) { ... } PROC implements Generic("-proc", ...) { ... } ... } Which is not too terrible (in fact has been put forward by John as a comment in the JEP [1]). That said, if we went down that path, note that, at least in my re-formulation, there's no way to view Option as a subtype of Generic. In other words, the enum as a whole would have nothing to do with Generic, subtyping wise. Now, I think for the particular use case we were considering, this could be still ok - at the end of the day we wanted to write something like: public Z get(Option option) { ... } and in this model we could rewrite this as: public Z get(Generic option) { ... } One observation, if we had sealed interface, one could do this: enum Option implements Consumer { ... sealed interface Generic { ... } } so that the interface can only be effectively implemented by the enum constants. So, what you're saying (or what I'm inferring from what you're saying :-)) is: if we can't have generic on enums, having custom generic supertypes on constants seems a pretty good approximation. Which is, I think, a fair point. [1] - https://bugs.openjdk.java.net/browse/JDK-8170351?focusedCommentId=14064981&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-14064981 On 15/06/17 12:43, Peter Levart wrote: > Hi Maurizio, > > What if the enum type was kept non-generic, but there could optionally > be a designated generic supertype inserted between the enum type and > the constant type. For example: > > public enum Option implements Consumer super Generic { > D("-d", ...), > PROC("-proc", ...), > ...; > > class Generic implements Function { > Generic(...) { > super(...); > } > > public T apply(String s) { > ... > } > } > > Option(...) { > ... > } > > public void accept(String s) { > ... > } > } > > > this would translate to: > > public class Option extends Enum