Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8267230

Implement ES6-style template literals in Java

XMLWordPrintable

      A DESCRIPTION OF THE PROBLEM :
      From what I can tell, this has not been suggested in the past.
      I'm proposing a version of template literals (documented in EMCAScript as follows: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals be implemented in Java.
      This might also use the backtick/grave (`) key rather than the traditional double quotation mark ("), or it would remain the same.

      Usage could be a number of possibilities but the one that sticks out to me would be something like the following:

      String piece = "hello world";
      String whole = "This is a ${piece} message!";

      Alternatively:

      String piece = "hello world";
      String whole = "This is a {piece} message!";

      or:

      String piece = "hello world";
      String whole = `This is a ${piece} message!`;

      That I can think of, the best way to do this would be to compile it as String#format (https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/lang/String.html#format(java.lang.String,java.lang.Object...)). This would ensure backwards compatibility.

      I personally would absolutely love to see this implemented because it would make code much more readable and less cluttered with strings like "example " + variable + " and so on."


            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: