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/comprehension: if clause is missing else #111

Closed
cueckoo opened this issue Jul 3, 2021 · 5 comments
Closed

spec/comprehension: if clause is missing else #111

cueckoo opened this issue Jul 3, 2021 · 5 comments
Labels
MaybeLater May be reopened and considered for later versions of the language. roadmap/language-changes Specific tag for roadmap issue #339

Comments

@cueckoo
Copy link
Collaborator

cueckoo commented Jul 3, 2021

Originally opened by @xinau in cuelang/cue#111

atm. the only way to implement a else is to add the same if clause and negating the expression

foo: ["dragons"]

if ((foo & string) != _|_) {
  bar: foo
} 
if ((foo & string) == _|_) {
  bar: 20
} 

while this works, it feels strange from a user perspective coming from other languages.
I also checked the spec and it seems to be missing https://github.com/cuelang/cue/blob/master/doc/ref/spec.md#comprehensions

@cueckoo cueckoo added MaybeLater May be reopened and considered for later versions of the language. roadmap/language-changes Specific tag for roadmap issue #339 labels Jul 3, 2021
@cueckoo cueckoo closed this as completed Jul 3, 2021
@cueckoo
Copy link
Collaborator Author

cueckoo commented Jul 3, 2021

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

It is not missing, it was deliberately omitted. I’m not entirely against adding it, but only after getting more data and experience with comprehensions and seeing how they develop. Conditional expression tend to lead to awful configurations, and although comprehensions are grammatically not expressions, they are getting close and just might develop into them.

@cueckoo
Copy link
Collaborator Author

cueckoo commented Jul 3, 2021

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

BTW, note that the above could be approximated as:

{
  foo: ["dragons"]

  {
    foo: string
    bar: foo
  } | {
    foo: [...] 
    bar: 20
  }
}

Disjunctions will often be a good approach for more complicated structural alternatives.

A more likely language extension, which would also be useful for JSON schema compatibility would be to allow !string or not(string) to mean null | bool | number | bytes | {} | [...].

@cueckoo
Copy link
Collaborator Author

cueckoo commented Jul 3, 2021

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

You could even fully simulate else with default values:

{
  foo: ["dragons"]

 *{
    foo: string
    bar: foo
  } | *{
    foo: int
    bar: 10
  } | {
    bar: 20
  }
}

where only the else block is not marked as a default. Note you can have multiple defaults, which makes sense to use if there is a discriminator field, like in this case.

@cueckoo
Copy link
Collaborator Author

cueckoo commented Jul 3, 2021

Original reply by @xinau in cuelang/cue#111 (comment)

@mpvl Thanks for the input.

@cueckoo
Copy link
Collaborator Author

cueckoo commented Jul 3, 2021

Original reply by @seh in cuelang/cue#111 (comment)

You could even fully simulate else with default values:

I discovered today that this example works up until the "foo" field has a value that's a string or an int. If it matches either of the first two disjunction alternatives, we have to ensure that it doesn't match the third as well.

We need something like the following in the third alternative:

foo: null | bool | bytes | float | [...] | {...}
bar: 20

ptMcGit pushed a commit to ptMcGit/cue that referenced this issue Jan 22, 2023
List it before Slack, on the basis we want to push people towards GH
Discussions (better surfaces questions and answers etc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
MaybeLater May be reopened and considered for later versions of the language. roadmap/language-changes Specific tag for roadmap issue #339
Projects
None yet
Development

No branches or pull requests

1 participant