A couple questions about String Templates

David Alayachew davidalayachew at gmail.com
Mon Oct 23 14:42:17 UTC 2023


Hello Amber Dev Team,

I have recently started playing with String Templates a lot more. They are
an excellent feature that makes handling Strings safer and easier than
before. I am pleasantly surprised.

As the number of my String Templates grows (currently 6 on a single
project), I am starting to think about how different String Template
Processors work together.

Currently, I am working on building a SWING_HTML String Template Processor,
which wraps the text into <html> tags, replaces new lines with <br>, and
does escaping. However, I also want to be able to do Swing HTML things,
like bolding, italicizing, and more. More specifically, I want to elevate
this functionality into the type system. My initial instinct is to delegate
the task to other processors. For example, SWING_HTML."Hello
\{BOLD."\{name}"}!"

However, one thing that I want to be able to do is to be able to ensure
that, if I do (for example) delegate bolding to another processor, then
that certain processor is only ever used with my SWING_HTML processor. As
in, a BOLD template processor does not make much sense if it is being used
outside of my SWING_HTML template processor.

I am not particularly attached to the delegating (don't want this to become
an XY problem). But the primary goal of being a one stop shop for all
things Swing HTML -- is that something I can do with String Templates,
where the individual components like bolding and italicizing can be lifted
into the Java syntax? Or is that asking too much/not worth the effort -- I
should just stick to writing tags manually/interpreting certain character
sequences as bold or italicized?

In the same vein, I have another question.

I am wondering if I am subtly drifting out of bounds of the goals of a
String Template. The syntax is attractive, so I find myself using it often.
However, I am starting to think that I am not using it for what it was
meant for.

For example, in my SWING_HTML processor, I find myself just passing in
arguments like so.

SWING_HTML."\{stringText}"

It's technically valid, but is this the sort of thing that should just be a
method instead? Is it more of a subtle border/gradient that we should
decide as a matter of trade offs?

Yet another question that builds off of the second one.

Oftentimes, we receive a String that we would like to break apart into
parts so that we can put it back together safely. Breaking it apart into
parts is not the responsibility of String Template Processor, but putting
it back together safely 100% is. Since String Templates are now (or very
soon to be) the de facto way of putting Strings together safely/properly,
what would be the the way of breaking them apart safely/properly?

This question about parallelism in strategies is born out of seeing how you
all are handling pattern-matching. Object construction and deconstruction
are parallels of each other. It sort of leads me to wonder if there is a
similar sort of play happening here for String deconstruction. My mind
wants to say java.util.regex.Pattern/Matcher, but I want to confirm that.

Thank you for your time and help!
David Alayachew
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20231023/cbee7727/attachment.htm>


More information about the amber-dev mailing list