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

spec: need to specify iteration order of struct comprehension #474

Open
cueckoo opened this issue Jul 3, 2021 · 2 comments
Open

spec: need to specify iteration order of struct comprehension #474

cueckoo opened this issue Jul 3, 2021 · 2 comments
Labels
Documentation FeatureRequest New feature or request NeedsDesign Functionality seems desirable, but not sure how it should look like. pre v1.0 roadmap/language-changes Specific tag for roadmap issue #339

Comments

@cueckoo
Copy link
Collaborator

cueckoo commented Jul 3, 2021

Originally opened by @myitcv in cuelang/cue#474

What version of CUE are you using (cue version)?

$ cue version
cue version +304b02e linux/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

exec cue eval

-- a.cue --
package x

m: banana: 4
-- b.cue --
package x

m: apple: 3
-- cue.mod/module.cue --
module: "mod.com"
-- x.cue --
package x

m: {
        aardvark: 5
}

l: [ for k, _ in m {k}]

What did you expect to see?

From the spec I'm unclear what to expect. My intuition is that the iteration order of the struct comprehension would be defined as [filename, offset].

What did you see instead?

m: {
    banana:   4
    apple:    3
    aardvark: 5
}
l: ["banana", "apple", "aardvark"]

which matches my intuition.

That said I think clarifying this in the spec is probably necessary?

See also discussion in #1348

@cueckoo cueckoo added Documentation FeatureRequest New feature or request NeedsDesign Functionality seems desirable, but not sure how it should look like. labels Jul 3, 2021
@cueckoo
Copy link
Collaborator Author

cueckoo commented Jul 3, 2021

Original reply by @mpvl in cuelang/cue#474 (comment)

Currently, the iteration order is not defined in the spec (unspecified) and thus up to the implementation.

In the old implementation, each field name had a unique id (assigned roughly in order of appearance) and the order was defined by this ID.

In the new implementation there is currently no ordering at all. I do want to implement merge sorting for unification (making it O(n)), after which this ordering would be reintroduced.

The new exporter, however, prints fields in topological order (or undefined if this is there is no topological sort). It probably makes sense to give some guarantees for this in the spec. Similarly, we could then also give some guarantees for iteration order.

@JensRantil
Copy link

FYI, #1896 (comment) contains a use-case for order-preserving iteration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation FeatureRequest New feature or request NeedsDesign Functionality seems desirable, but not sure how it should look like. pre v1.0 roadmap/language-changes Specific tag for roadmap issue #339
Projects
None yet
Development

No branches or pull requests

4 participants