Skip to content

Multi-statement closure type inference #32223

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

Closed

Conversation

DougGregor
Copy link
Member

@DougGregor DougGregor commented Jun 6, 2020

Introduce support for type inference of closures with multiple
statements. Type inference flows from the closure parameters through
the body of the closure to determine the return type. This, for example,
allows a "map" closure with multiple statements to infer the result type
based on the "return" in the body:

let _ = ints.map { i in
  let value = i + 1
  return String(value)
}

The frontend option -experimental-multi-statement-closures enables
this new behavior. It should cover all statement and declaration kinds
in a closure now:

  • Expressions
  • Brace statements
  • var/let declarations (note: only when there is also an initializer)
  • do statements (without a catch)
  • do statements (with catch clauses)
  • if statements
  • guard statements
  • while loops
  • repeat-while loops
  • for..in loops
  • throw statements
  • defer statements
  • break and continue
  • switch statements
  • #assert statements
  • Other kinds of declarations (local types, etc.)

@DougGregor DougGregor marked this pull request as draft June 6, 2020 05:59
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor DougGregor force-pushed the multi-statement-closures branch from 17b0fcd to 49b37f9 Compare June 7, 2020 05:02
@DougGregor
Copy link
Member Author

@swift-ci smoke test

@DougGregor
Copy link
Member Author

@swift-ci smoke test macOS

@DougGregor
Copy link
Member Author

@swift-ci please smoke test

Introduce support for type inference of closures with multiple
statements. Type inference flows from the closure parameters through
the body of the closure to determine the return type. This, for example,
allows a "map" closure with multiple statements to infer the result type
based on the "return" in the body:

    let _ = ints.map { i in
      print(i)
      return String(i)
    }

The frontend option `-experimental-multi-statement-closures` enables
this new behavior. Only simple expression statements in closures work
thus far.
@DougGregor DougGregor force-pushed the multi-statement-closures branch from 5f73625 to 9e22826 Compare August 15, 2020 05:57
@DougGregor
Copy link
Member Author

@swift-ci smoke test

@shahmishal
Copy link
Member

Please update the base branch to main by Oct 5th otherwise the pull request will be closed automatically.

  • How to change the base branch: (Link)
  • More detail about the branch update: (Link)

@shahmishal shahmishal closed this Oct 5, 2020
@xwu
Copy link
Collaborator

xwu commented Oct 5, 2020

Darn, was really pumped about this. Any chance it’ll be resurrected?

@IanKeen
Copy link

IanKeen commented May 18, 2021

same, I hope this makes a comeback! :(

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