RFR: 8323601: Improve LayoutPath.PathElement::toString

Maurizio Cimadamore mcimadamore at openjdk.org
Mon Jan 15 10:42:21 UTC 2024


On Mon, 15 Jan 2024 07:57:00 GMT, Per Minborg <pminborg at openjdk.org> wrote:

>> This PR proposes to add an improved Improve `LayoutPath.PathElement::toString` method simplifying debugging.
>> 
>> Opinions and suggestions for `static PathElement sequenceElement(long start, long step)` are welcome.
>
> test/jdk/java/foreign/TestLayoutPaths.java line 341:
> 
>> 339:     public void testSequenceElementRangeToString() {
>> 340:         PathElement e = PathElement.sequenceElement(2, 4);
>> 341:         assertEquals(e.toString(), "sequence range 2 + N * 4, N >= 0");
> 
> Not sure this is the best way to express the toString for the range version.

I think there's some choices here. One way possible way to think about paths is to imagine the consequences of chaining the strings. In the proposed implementation, this would yield very good/hard to parse results, I think.

Another way would be to borrow some C syntax, to make it look more like an access expression, so that we we can see something like:


a.b[23].c[].d->e


That said, while the above looks nice to read, it's probably hard to generate, because when printing out a single element we don't know whether e.g. to emit `.` or `->` (as a separator).

But, we could still take some inspiration from that, and have the following string representations:

* `a`
* `b[23]`
* `c[]`
* `d`
* `*e`

Then if a client chains everything, they can use e.g. `/` (or `.`) as a delimiter and it will print out something sensible and compact.

Back to your question, for the sequence range expression we can use a Numpy-style syntax:

`<start index> : <step>`

So, in this case:

`[2:4]`

Which is, again, relatively compact and "composable".

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/17417#discussion_r1452210277


More information about the core-libs-dev mailing list