-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Qute: prefix loop metadata #20671
Comments
/cc @mkouba |
I agree that the docs should be improved.
I don't think it's a big problem. It will never override a property of an iterated element which is what matters in 95% of cases. It can override some template data or a scope variable defined by
Yes, that would make no sense.
Well, it would reduce the probability of a collision but a name prefixed with |
Hm, the more I think about this, the more I think it's not worth the effort. I mean we should definitely introduce the metadata properly in the docs (right now there's only one example and a TIP that lists all properties). But I'm not so sure about the change. We could make it configurable, globally or per loop, but I doubt that someone will ever use it. We could introduce a prefixed version but then we would have to keep the old version for backward compatibility and so we would still override the variables from the outer scope. |
You could add a syntax to declare a new "object" that would contain the metadata. Something like:
I don't know about |
Yes, I've been thinking about something like
Yeah, "needless syntax sugar" everywhere! I personally like the |
Choosing where to stop with adding syntax sugar is certainly one of the more artistic parts of language design :-) |
PR to improve the docs: #20687 |
Honestly, IMO the best bet is to prefix it with the iterated variable name, because it greatly reduces shadowing issues (
|
Ok, we could easily implement this but like I said we'll have to keep the original approach for backwards compatibility, or at least provide some way of switching back to the old behavior... |
I'm not entirely sure it's worth the effort, but is there any way to keep the old behaviour at the same time as the new, and issue a warning for deprecation if we detect someone using the old behaviour? |
We can log a warning whenever the old-style is used at runtime but that could be really annoying. I don't think there's an easy and reliable way to detect the problem when analyzing the templates, e.g. during build. |
Then perhaps yes, a new property to enable the old behaviour, defaulting to false? I don't know how many people use this. |
Ok, let's make it configurable. It will be a breaking change so we'll target 2.5.
I believe that it's a commonly used functionality... |
OK thanks! |
Please make sure we have a very actionable error message so that people know how to fix it. I'm pretty sure it's often used, given how often you have to special case the first or last iteration. |
While implementing the change I ran into a problem: we're not able to detect an invalid usage of iteration metadata keys at runtime without false positives. For example, if a template looks like The truth is that for type-safe templates with |
- the prefix is configurable (globally) - also added "isFirst" and "isLast" properties for convenience - resolves quarkusio#20671
- the prefix is configurable (globally) - also added "isFirst" and "isLast" properties for convenience - resolves quarkusio#20671
- the prefix is configurable (globally) - also added "isFirst" and "isLast" properties for convenience - resolves quarkusio#20671
- the prefix is configurable (globally) - also added "isFirst" and "isLast" properties for convenience - resolves quarkusio#20671
- the prefix is configurable (globally) - also added "isFirst" and "isLast" properties for convenience - resolves quarkusio#20671
- the prefix is configurable (globally) - also added "isFirst" and "isLast" properties for convenience - resolves quarkusio#20671
- the prefix is configurable (globally) - also added "isFirst" and "isLast" properties for convenience - resolves quarkusio#20671
Description
https://quarkus.io/guides/qute-reference#loop_section says that we get iterator metadata
index
,hasNext
,odd
andeven
, but it does not show any example of how to access it, leading me to believe it would be a property of the iterated variable such asit.index
. I now realise it showed this example:Which introduces the
count
metadata, I guess, but because it's not properly introduced I assumed it was a random toplevel.The docs should be more clear as to which metadata properties are available, and also this is terrible wrt scoping, because it will automatically override any explicitely declared binding named
count
orindex
by surprise.I do agree it'd be weird to tack these metadata properties to the iterated variable.
In other templates, I've seen the iterated variable name being used as a prefix, though, such as
it_count
ormyVar_hasNext
which greatly limits binding clashes.Could we get such prefixed properties instead?
Implementation ideas
No response
The text was updated successfully, but these errors were encountered: