Skip to content

feat: switch - #1278

Merged
aljazerzen merged 1 commit into
mainfrom
switch
Dec 15, 2022
Merged

aljazerzen merged 1 commit into
mainfrom
switch

Conversation

@aljazerzen

Copy link
Copy Markdown
Member

First stab at #504

from employees
derive display_name = switch [
    nickname != null -> nickname,
    true -> f'{first_name} {last_name}'
]

Note that:

  • default case can be set by using true condition.
  • if there is no true condition, default is NULL (unlike in SQL where an exception is the default).

@aljazerzen
aljazerzen enabled auto-merge (squash) December 15, 2022 19:58
@aljazerzen
aljazerzen requested a review from max-sixty December 15, 2022 19:58
@aljazerzen
aljazerzen merged commit e79f428 into main Dec 15, 2022
@aljazerzen
aljazerzen deleted the switch branch December 15, 2022 20:00
@max-sixty

Copy link
Copy Markdown
Member

Amazing — huge win! (and such a small PR!)

@snth snth added this to the 0.4 milestone Dec 21, 2022
@vanillajonathan

Copy link
Copy Markdown
Collaborator

Isn't it rather misleading to use true for this?
Then you would assume it would use that branch when it was actually literally true?
It makes no sense that true is used as the default case.

I propose instead to do it the way Rust does and use _ to mean for anything else.

@max-sixty

Copy link
Copy Markdown
Member

Then you would assume it would use that branch when it was actually literally true?

This isn't matching against a value, it's switching based on which condition is true; does that make sense?

@vanillajonathan

Copy link
Copy Markdown
Collaborator

Then you would assume it would use that branch when it was actually literally true?

This isn't matching against a value, it's switching based on which condition is true; does that make sense?

Oh okay, I think I get it now, but it is a bit unconventional and different from the C-style switch and the Rust-style match that I am used to. Seeing the name switch might lead someone to believe it would work something like a C-style switch statement. Despite sharing the same name it works entirely different.

An alternative could be:

from employees
derive display_name = condition [
    if nickname != null -> nickname,
    else -> f'{first_name} {last_name}'
]

This would avoid confusion by not using the name name "switch" , and instead using some other name such as "condition". Using the keywords if and else could also further be used to make it easier to grasp.

@max-sixty

Copy link
Copy Markdown
Member

I actually think you're right about the switch statement. I'm not sure why I missed that before. I think we were going between switch and match and case, and deciding whether there was a control value, and we ended up here.
Note that SQL uses case like we use switch, though other languages use case as part of a traditional switch statement.

We did spend a lot of time discussing this, and we've spent a lot of time discussing the syntax (!), so maybe folks' patience for more discussions is waning. But I would propose we either:

  • Rename switch to something like case
  • or have a control value, which could be true to get the current behavior, or could be foo to evaluate against foo
  • or some other synthesis of the tradeoffs

I'm less of a fan of the if / else here fwiw, though there are reasonable alternative constructions of if & else)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants