Hi Tagir, Apologies for not replying sooner. Yes, this is an excellent suggestion - improving the precision of typing template expressions is a very welcome change :-)
I have updated the spec, and the compiler will be updated shortly. Thanks, Gavin > On 10 Oct 2023, at 06:33, Tagir Valeev <[email protected]> wrote: > > Hello, experts! > > Probably it's possible to fix the small thing I wrote before? Namely, > the type of template expression and its thrown exceptions are > determined by the template expression class signature (namely, the > instantiation of R and E generic parameters of Processor) and ignoring > the actual signature of the process() method, which may have covariant > return type and thrown exception type declared. This looks out of sync > with the rest of Java. Example: > > public final class TemplateTest { > interface AnyProcessor extends StringTemplate.Processor<Object, > Throwable> {} > > static class IntProcessor implements AnyProcessor { > @Override > public Integer process(StringTemplate stringTemplate) { > return 123; > } > } > > public static void main(String[] args) { > // error: incompatible types: java.lang.Object cannot be > converted to java.lang.Integer > Integer xyz = new IntProcessor()."hello"; > // error: unreported exception java.lang.Throwable; must be > caught or declared to be thrown > Object xyz2 = new IntProcessor()."hello"; > } > } > > I'm not sure whether current behavior is explicitly stated in the spec > draft, but the reference implementation behaves this way. I would > expect this code to be compilable. > > Note that we have a similar implicit method invocation construct, > try-with-resources, and it respects the actual signature of the > close() method (namely, declared exceptions). I think that similarly > we should respect the actual method signature here. > > With best regards, > Tagir Valeev. > > On Mon, Oct 9, 2023 at 4:46 PM Gavin Bierman <[email protected]> wrote: >> >> Dear experts: >> >> The first draft of a spec covering JEP 459 (String Templates (Second >> Preview)) is now available at: >> >> https://cr.openjdk.java.net/~gbierman/jep459/latest >> >> Feel free to contact me directly or on this list with any comments. >> >> Thanks >> Gavin >> >> >>> On 5 Oct 2023, at 13:20, Mark Reinhold <[email protected]> wrote: >>> >>> https://openjdk.org/jeps/459 >>> >>> Summary: Enhance the Java programming language with string >>> templates. String templates complement Java's existing string literals >>> and text blocks by coupling literal text with embedded expressions and >>> template processors to produce specialized results. This is a preview >>> language feature and API. >>> >>> - Mark
