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

Nested Destructuring #593

Merged
merged 17 commits into from
May 9, 2023
Merged

Nested Destructuring #593

merged 17 commits into from
May 9, 2023

Conversation

gdotdesign
Copy link
Member

@gdotdesign gdotdesign commented May 5, 2023

This implements a recursive algorithm for destructuring and unifies statement and case branch destructuring checks. This allows:

  • Destructuring with values in them
  • Mixing and nesting destucturings
  • Using complex destructurings in if expressions

Some examples:

case Test::Record(name: Maybe::Just({"John", "Doe"}), age: 32) {
  Test::Record(Maybe::Just({"John", a})) => a
  => "WTF"
}
    
case Result::Ok(Maybe::Just({"Hello", "Bello"})) {
  Result::Ok(Maybe::Just({"Hello", b})) => "Matches"
  Result::Ok(Maybe::Just({"JULIE"})) => "Not matches"
  Result::Err => "Error"
  => "Else branch"
}

case ["a", "b", "d"] {
  [a, ...b, d] => b
  ["a"] => ["one"]
  [a] => ["something else"]
  [] => ["empty"]
}

if Result::Ok(Maybe::Just({"Hello", b})) = result {
  ...
} else {
  ...
}

Top level exhaustiveness checking is still there but if any branch has nested destructuring or value ("string" for example) in it then it's excluded from the check.

Follow-up work (in separate PRs):

  • Move _match and related functions into the runtime
  • Allow named destructuring of records (including in enums)
  • At some point implement exhaustiveness checking in sublevels

@gdotdesign gdotdesign added enhancement New feature or request language Language feature labels May 5, 2023
@gdotdesign gdotdesign added this to the 0.18.0 milestone May 5, 2023
@gdotdesign gdotdesign self-assigned this May 5, 2023
core/source/Http.mint Outdated Show resolved Hide resolved
spec/compilers/case Show resolved Hide resolved
src/parsers/variable.cr Outdated Show resolved Hide resolved
@Sija
Copy link
Member

Sija commented May 5, 2023

@gdotdesign Does it resolves #283?

@gdotdesign
Copy link
Member Author

@gdotdesign Does it resolves #283?

Yes it will :)

@Sija Sija linked an issue May 5, 2023 that may be closed by this pull request
@gdotdesign gdotdesign marked this pull request as ready for review May 8, 2023 05:18
@Sija Sija linked an issue May 8, 2023 that may be closed by this pull request
src/compilers/top_level.cr Outdated Show resolved Hide resolved
src/parsers/variable.cr Outdated Show resolved Hide resolved
src/type_checkers/case.cr Outdated Show resolved Hide resolved
src/type_checkers/case.cr Outdated Show resolved Hide resolved
src/type_checkers/case.cr Outdated Show resolved Hide resolved
Co-authored-by: Sijawusz Pur Rahnama <sija@sija.pl>
src/type_checkers/case.cr Outdated Show resolved Hide resolved
Copy link
Member

@Sija Sija left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src/parsers/variable.cr Outdated Show resolved Hide resolved
src/parsers/variable.cr Outdated Show resolved Hide resolved
@gdotdesign gdotdesign merged commit 3993a09 into master May 9, 2023
@gdotdesign gdotdesign deleted the nested-destructuring branch May 9, 2023 10:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request language Language feature
Development

Successfully merging this pull request may close these issues.

Cannot pattern match on Bool literal in a Tuple Destructure more complicated data structures
2 participants