From asviraspossible at gmail.com Wed Apr 17 02:19:06 2019 From: asviraspossible at gmail.com (Victor Nazarov) Date: Wed, 17 Apr 2019 02:19:06 +0000 Subject: String reboot proposal Message-ID: I want to propose one more design vector for enhanced string literals that I haven't seen before Basically, I propose two extensions to get to multiline literals with incidental white space handling. Extension1: allow sequences of string literals to denote a single concatenated literal Extension2: allow string literals that spans from an opening token till the end of a line Let's start with an example: String j = "" + "public static void " + name + "(String... args) {\n" + " System.out.println(\"Hello,\\t\" + String.join(args));\n" + "}\n"; Extension1: allow sequence of string literals to denote concatenated literal With this extension we can get rid of plus operators, like this: String j = "" "public static void " + name + "(String... args) {\n" " System.out.println(\"Hello,\\t\" + String.join(args));\n" "}\n"; This feature is not so alien, it is present in C language. It may seem not worth it, but I think it can be worth it in combination with second extension. Extension2: allow string literals that spans from an opening token till the end of a line. String s = "hello\n"; to be written as String s = """hello ; Tripple quotes in this case start a string literal which ends at an end of line. Having these two extensions, we can rewrite our example as: String j = "public static void " + name + """(String... args) { """ System.out.println("Hello,\\t" + String.join(args)); """} ; Other examples: String sql = """SELECT name """FROM user """WHERE id = ? and role = 'ADMIN' ; String json = """{ """ "login": "john", """ "id": 123. """} ; String html = """
""" Hello, World """
; With this style we can't just copy and past snippets of foreign code into a string literal, but all we need is to prefix every line with tripple quotes. This style is analogues to source code comments handling by programmers, so, I think, it's familiar enough. // TODO: rewrite this method // using new guava API Automatic prefixing of comments is already present in every IDE and is simple enough to implement by hand, like | sed 's/^/"""/g' Rawness can be added to such string literals independently in orthogonal way: String regex = \"a(bc)+\s+(de|kg)?"\; String j = \"public static void "\ + name + \"""(String... args) { \""" System.out.println("Hello,\t" + String.join(args)); \"""} ; The problem I see is interoperability between simple string literals and "line sting literals": we can't easily align them vertically. But may be this problem is more easily solvable than magical incidental whitespace handling. -- Victor Nazarov From sstewartgallus at sstewartgallus.com Sat Apr 27 00:26:21 2019 From: sstewartgallus at sstewartgallus.com (Steven Stewart-Gallus) Date: Fri, 26 Apr 2019 17:26:21 -0700 Subject: How will record destructing be implementable without multiple return values? Message-ID: <79a1d4ec-e1ed-dd90-8189-cdae280e1803@sstewartgallus.com> Hello, It seems like there are potential plans for record destructuring but not fully hashed out yet in a formal JEP https://cr.openjdk.java.net/~briangoetz/amber/pattern-match.html? The formal JEPs https://openjdk.java.net/jeps/305 seem to be much more scaled back. Record destructuring doesn't seem implementable efficiently to me on the JVM as shown.? I guess this is why the JEP was scaled back? Suppose we have a Point class and we want to destruct in radians format. switch (obj) { case Point.ofRadians(var theta, var radius): ??????? return doStuff(theta, radius); } How can you return multiple values to make this work without allocating?? Or is this just planned to be put on the back burner until things like Project Valhalla go through which would make this much less of an issue.? I think an encoding like: public static Synthetic ofRadians(Object obj) { ??? if (obj instanceof Point p) { ??????????? return new Synthetic(true, p.theta(), p.radius()); ??? } ??? return new Synthetic(false, 0, 0); } public inline record Synthetic(int matched, int theta, int radius) { } might work but it seems silly. Or am I missing something and this isn't really a problem. Thank you, Steven Stewart-Gallus