Skip to content
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

Array filter: Positional access #1207

Closed
ChristianGruen opened this issue May 13, 2024 · 2 comments · Fixed by #1217
Closed

Array filter: Positional access #1207

ChristianGruen opened this issue May 13, 2024 · 2 comments · Fixed by #1217
Labels
Enhancement A change or improvement to an existing feature PR Pending A PR has been raised to resolve this issue Tests Needed Tests need to be written or merged XPath An issue related to XPath

Comments

@ChristianGruen
Copy link
Contributor

I apologize for warming up an already accepted feature, but I have mixed feelings about the deviating rules for filters and array filters. People can do…

for $n in $numbers[1 to 5]
return $n + 1

…but they won’t be able to do…

for member $n in $numbers?[1 to 5]
return $n + 1

Of course you can always use array:subarray, array:slice, etc., but from the perspective of symmetry and usability, it’s just not obvious why positions are exclusively allowed for sequences. Even if we regard numeric predicates as a design error, we should rather deliberately repeat other people’s mistakes

@ChristianGruen ChristianGruen added XPath An issue related to XPath Enhancement A change or improvement to an existing feature labels May 13, 2024
@michaelhkay
Copy link
Contributor

michaelhkay commented May 13, 2024

The reason I left out numeric subscripting is that it's not clear whether it should return a single member or an array of members.

You would probably expect

let $array := [1, 2, 3, 4, 5] return $array?[3]

to return 3 rather than [3].

Overloading filtering and subscripting works for sequences because an item is the same thing as a sequence of length 1. That equivalence doesn't exist for arrays, so if you overload the operator there's immense scope for confusion.

As the spec says:

Unlike filter expressions for sequences, there is no special treatment of numeric predicate values. This is because it would be unclear whether such an expression should return the member at the specified position, or a singleton array containing that member. Numeric values for the predicate are disallowed to avoid any possible confusion.

@ChristianGruen
Copy link
Contributor Author

ChristianGruen commented May 13, 2024

Unlike filter expressions for sequences, there is no special treatment of numeric predicate values. This is because it would be unclear whether such an expression should return the member at the specified position, or a singleton array containing that member. Numeric values for the predicate are disallowed to avoid any possible confusion.

I’ve seen the comment, but I don’t find it persuasive. [$n] and [position() = $n] have always been equivalent; if we believe that this syntax could be immensely confusing for arrays and possibly maps, we should definitely drop positional access in array/map predicates in general. Next, now that we allow EXPR[$n1, $n2, ...], the single integer is just a special case.

My preference, however, would be to make predicates completely symmetric, no matter if they are part of XPath steps, Filter Expressions or Array/Map Filters.

@michaelhkay michaelhkay added PR Pending A PR has been raised to resolve this issue Tests Needed Tests need to be written or merged labels May 14, 2024
@ndw ndw closed this as completed in #1217 May 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement A change or improvement to an existing feature PR Pending A PR has been raised to resolve this issue Tests Needed Tests need to be written or merged XPath An issue related to XPath
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants