-
Notifications
You must be signed in to change notification settings - Fork 15
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
Support sequence, array, and map destructuring declarations #37
Comments
I like the approach. Deconstruction is a powerful feature in other programming languages, and I see many use cases for it in XQuery. If a sequence is deconstructed, I would indeed suggest assigning all remaining items to the last variable: let $(a, b, c) := 1 to 4
return $c (: 3, 4 :) @rhdunn I assume you also had this in mind in the CSV example? |
Yes, that's what I had in mind with the CSV example. It should also apply to arrays and maps -- if there is one value left, assign that value to the last variable; otherwise assign an array/map with the remaining values to the variable. |
+1 for arrays. Maybe it’s not really required for maps (and I’m not sure how that’s supposed to look like?). I would guess that unreferenced keys won’t be relevant to the user most of the time. An example: let $inc-x := function($coords) {
map:put($coords, 'x', $x?x + 1)
}
let $coords := map { 'x': 1, 'y': 3, 'z': 0 }
let $(x, y) := $inc-x($coords)
return $x + $y |
You are right, it would make more sense to ignore unreferenced/unnamed keys for maps. |
I have to confess I've never really seen the attraction of this feature in other languages, it's one that I tend not to use. That probably inclines me to the view that it's not needed. I can see the attraction with maps/records - with the introduction of record types we're starting to introduce the idea that the names of the entries in a map might be known statically and that we can take advantage of this knowledge to make programs more readable and more statically checkable, and there's some attraction in being able to write
in preference to
In XSLT I guess it could be For sequences and arrays I'm less convinced, because I don't particularly want to encourage these data types to be used in a way where the semantics are non-uniform, e.g. where items 1 and 2 in a sequence/array mean different things and are used in different ways. I think the right structure for that kind of job is a map. I would always use a map, for example, for coordinates in three-dimensional space, not a sequence or an array. |
The benefit of this feature to me is in being able to make it clear what the intent is (i.e. I'm only interested in these components), can give meaningful names to things, and write that in less code. If the item is being referred to in multiple places, I can avoid repeating the code. Regarding arrays and sequences, one key use case would be the CSV example above where you want to filter out the headers (first row of the CSV) from the rest of the CSV. There may be other cases where this is useful -- especially for existing functions (which is a stated goal with the variadic argument work) such as MarkLogic's xdmp:tidy which returns the errors/warnings in the first item and the tidied result in the second. For example:
I don't think the language should be constraining a feature to any one type because there is no perceived use for that, or because it is generally discouraged. It creates gaps in the language, resulting in people asking why does one feature support sequences and arrays, another maps only, and a third all three, when there is no technical reason preventing such usage. |
It feels to me like bad data modelling to use the first item in a sequence for headers, and subsequent items for data. I don't think we should be providing features to make it easier to manipulate data that's badly designed. Just because it was designed that way before maps/tuples were available doesn't really change that argument. |
One popular use case for using deconstruction is the access to the head and tail of a sequence, which potentially allows you to save one extra variable binding: (: old :)
let $data := ...
let $head := head($data)
let $tail := tail($data)
(: new :)
let $(head, tail) := ... But I share your concerns: Deconstruction is also misused as quick’n’dirty replacement for cleaner data structures. |
@rhdunn , When I proposed the lazy hint @michaelhkay was asking if this proposal also covered the so called incremental evaluation (like evaluate only the At that time I didn't remember the exact syntax of destructuring that you proposed, so I ventured with:
Now I am re-reading your proposal, and it seems that it will be more like:
Or even:
Could you, please, advise us for what you consider the best/recommended syntax of destructuring to indicate partial/incremental evaluation? |
let ($a, $b, $c) := matches($input, '(\d+), and (\d+) gives (\d+);) |
@rhdunn What would be the syntax for destructuring an array or a sequence into head/tail where we need only the head? I suppose something like: let $[h, _] := $myArray or perhaps: let $[h, _] := $myArray ! (head(), tail()) I support the use of the _ (underscore) character as a wildcard symbol. Furthermore, in the spirit of the lazy keyword , I propose that any such wildcard symbol is by definition lazy, thus we don't have to write: let $[h, lazy _] := $myArray ! (head(), tail()) because by definition it has exactly the same meaning if the lazy keyword is omitted. |
Hi @dnovatchev, personally, I think it’s misleading to call an operation lazy if it will never happen. Think of e.g. Next, I believe it’s usually not the assignment of the result that’s expensive, but the evaluation of the expression itself. There are many cases, such as sorting, that require the full value to be computed before an assignment can take place: let $(head, tail) := sort($data) If the tail of a value is irrelevant, I would usually expect the optimizer to take care of the advanced work. The following lat clause… let $(head, _) := EXPENSIVE
return $head …could easily be rewritten by the compiler to: let $head := head(EXPENSIVE) On the other hand, this might be done by the user anyway, so I’m not sure if we need a placeholder for unused values? Maybe we could define 2, 3 simple use cases for that possible requirement? |
There is a bit of bike-shedding around the syntax:
|
@ChristianGruen |
That would certainly be helpful. Which languages do you know that provide placeholders for unused values? |
From Kotlin: https://kotlinlang.org/docs/destructuring-declarations.html#underscore-for-unused-variables. |
For C#: |
So let's try to summarise what's being proposed.
is equivalent to and using "_" as a variable name causes no binding to take place for that position.
is equivalent to and using "_" as a variable name causes no binding to take place for that position. (or do we want to do the equivalent without bound-checking?) Note that a and b are of type item()* while c is of type array(item()*). But that doesn't seem to work for the case where you know the array has length 3 and you want to bind $a, $b, and $c to its three members.
is equivalent to and in this case x and y must be NCNames (because using QNames gets too complicated). Have I got that right? |
I often construct maps with QName keys. This is based on the practice introduced, I think, in XSLT many years ago, that values in no namespace are reserved for the specification and any extension values have to be in a namespace. That's used throughout XProc, for example, where maps are used for things like output options. I've used destructuring in other languages, like Scala. I confess I don't ever recall ever writing an XPath expression and thinking "gee, I wish I could use destructuring here" but that's as likely because I've learned XPath without it as anything else. I'm not opposed to adding destructuring. But I think I am opposed to adding it if it's limited in ad hoc ways. If I can destructure a map with string keys, I should be able to destructure a map with QName keys. Or perhaps destructuring simply shouldn't apply to maps. |
It feels restrictive to me to reserve a string that is a legal variable name. What about using the dash character let $[-, y, -] := camera-angle()
+1 for bound checks.
I think we should avoid using different item types for the variables (the cardinality may be different, though). We could wrap all members into arrays: let $[x, y] := [ 1, (2, 3), 4 ]
return (
$x (: [ 1 ] :),
$y (: [ 2, 3 ], [ 4 ] :),
) This way, we could write: (: moves the first member of an array to the end :)
let $[x, y] := array { ... }
return array:join(($y, $x)) Array entries could be another option (#314). I hope my interpretation of the syntax is correct? let $[x, y] := [ 1, (2, 3), 4 ]
return (
$x (: map { 'value': 1 } :),
$y (: map { 'value': (2, 3) }, map { 'value': 4 } :),
) As sequences and arrays are handled differently, and as we have bound checks, we could be strict and raise an error if the number of variables and members differs at runtime: let $[x, y] := [ 1, 2, 3 ] (: error :) We could also treat maps and arrays similarly and ignore all members for which no variables are declared: let $[x, y] := [ 1, 2, 3, 4, 5 ]
return $y (: 2 :) |
@ChristianGruen Note that there is nothing special about the use of
I used I'm happy to come up with an unused specifier if you (or other processor implementors) think that the current proposal does not give you enough information to elide the expansion. -- I think something in this regard would make sense as the processor then does not need to emit a corresponding let expression, figure out that it is not used, and then remove the let expression it just added. |
I think the problem with that is that it requires a complicated rule saying you can only include the same variable name twice in a declaration if the variable is never used. Without that rule, we'd be allowing you to bind the same variable to two different things. Alternatively, I guess we could just say "last one wins", as it does when you say "let $x := 3, $x := 4". |
I'm advocating a "last one wins" approach -- i.e. all this proposal is is syntactic sugar for a multi-let assignment to parts of a sequence/array/map. |
An alternate syntax discussed in #31 is:
and equivalent for syntax. This would require adding |
Given a function that returns a sequence, array, or map of a fixed length or structure, it would be useful to extract those values in a destructuring declaration like can be done in other languages (such as JavaScript, Kotlin, C++, and Python). For example:
These would be equivalent to:
It should be possible to define the type of a component and/or the whole construct:
For maps, it would also be useful to rename the components, such as:
It should also be possible to capture any left-over items in the sequence/array/map, for example:
A destructuring declaration should be usable anywhere a variable binding can be defined.
It should not be an error to use the same variable name twice. This supports conventions such as using
_
for unused values. For example:The text was updated successfully, but these errors were encountered: