paulk-asert commented on PR #2580:
URL: https://github.com/apache/groovy/pull/2580#issuecomment-4664134906
> The key reframing: under the **dynamic** runtime, `m['k']` already means
`m.get('k')` for every key — `[:].class`, `[:].properties`, `[:].metaClass` all
return `null` (the absent entry), not the bean. The surprising behavior was
confined to **static** resolution, where subscript was binding to the
property-access overload. So this PR isn't trading one consistency for another
so much as **realigning STC subscript with the long-standing dynamic
semantics** (and with `get`/`put`).
>
> Verified values (dynamic against the runtime; static column = intended 6.0
/ the STC test assertions in this PR):
>
> **Plain map** — `[foo:'bar']` / `[class:'C']` / `[:]`
>
> | key | `m.k` dynamic | `m.k` `@CompileStatic` | `m['k']` (6.0) |
`m.get('k')` |
> |---|---|---|---|---|
> | ordinary (`'foo'`) | `'bar'` | `'bar'` | `'bar'` | `'bar'` |
> | meta-name present (`'class'`→`'C'`) | `'C'` (entry, not the Class) |
`'C'` | `'C'` | `'C'` |
> | meta-name absent (`'class'`) | `null` | `null` | `null` | `null` |
>
> **Map subclass** — `class C extends HashMap { def foo = 1; def bar = 2 }`,
then `put('foo',11)`, `put('baz',33)`
>
> | key | `m.k` dynamic | `m.k` `@CompileStatic` | `m['k']` (6.0) |
`m.get('k')` |
> |---|---|---|---|---|
> | declared prop **and** entry (`'foo'`: prop=1, entry=11) | **`11`**
(entry wins) | **`1`** (property wins) | `11` | `11` |
> | declared prop, **no** entry (`'bar'`: prop=2) | **`null`** (property
ignored) | **`2`** (property) | `null` | `null` |
> | entry, **no** declared prop (`'baz'`: entry=33) | `33` | `33` | `33` |
`33` |
>
> Two takeaways:
>
> 1. **After this PR, `m['k']` ≡ `m.get('k')` in every row and both modes**
— one clean column, no asterisks. That's the win worth stating explicitly as
the invariant.
> 2. The genuinely treacherous cell is **`m.k` on a Map subclass**, which
disagrees dynamic-vs-static (entry wins dynamically, declared property wins
under STC). This PR doesn't touch dot access, so that divergence remains —
subscript just sidesteps it now. Might be worth a follow-up issue, or at least
a doc note steering entry access to `[]`/`get`.
>
> Suggestion for locking it in: pin the invariant as a paired test that runs
the same asserts under dynamic **and** `@CompileStatic`, so the static/dynamic
gap that caused this can't silently reopen via overload-resolution drift. Happy
to put that together if helpful.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]