Skip to content

ArrayExprSyntax initializer that takes an array of ExprSyntax #1674

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
grynspan opened this issue May 17, 2023 · 5 comments · Fixed by #1693
Closed

ArrayExprSyntax initializer that takes an array of ExprSyntax #1674

grynspan opened this issue May 17, 2023 · 5 comments · Fixed by #1693
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@grynspan
Copy link
Contributor

Description

Constructing an ArrayExprSyntax from a Swift array of arbitrary ExprSyntax values is a bit of a pain. Right now, we do silly dances like:

let exprs: [ExprSyntax] = ...
let exprString = exprs.lazy
  .map(\.trimmedDescription)
  .joined(separator: ", ")
return ExprSyntax("[\(raw: exprString)]").as(ArrayExprSyntax.self)

Having a convenience initializer that inserts commas and necessary trailing trivia would make this much nicer.

@grynspan grynspan added the enhancement New feature or request label May 17, 2023
@ahoppen ahoppen added the good first issue Good for newcomers label May 17, 2023
@ahoppen
Copy link
Member

ahoppen commented May 17, 2023

Tracked in Apple’s issue tracker as rdar://109483386

@gibachan
Copy link
Contributor

Is it okay for me to handle this issue?

@kimdv
Copy link
Contributor

kimdv commented May 22, 2023

Hi @grynspan!

Would you be able to achieve your request with the result builder initialiser of ArrayExprSyntax

Maybe something like

return ArrayExprSyntax {
    for item in [1, 2, 3, 4] {
        ArrayElementSyntax(expression: ExprSyntax("\(raw: item)"))
    }  
}

@ahoppen
Copy link
Member

ahoppen commented May 22, 2023

Is it okay for me to handle this issue?

Sure, I assigned it to you.

Would you be able to achieve your request with the result builder initialiser of ArrayExprSyntax

Maybe something like

return ArrayExprSyntax {
    for item in [1, 2, 3, 4] {
        ArrayElementSyntax(expression: ExprSyntax("\(raw: item)"))
    }  
}

To speak on @grynspan’s behalf: The motivating example here is where you already have an [ExprSyntax]. You could do the following but that seems kind dumb

ArrayExprSyntax {
  for element in arrayElements {
    element
  }
}

@grynspan
Copy link
Contributor Author

Would you be able to achieve your request with the result builder initialiser of ArrayExprSyntax

Yes! However it seems like a convenience initializer here is worth implementing, because "constructing an ArrayExprSyntax from an array of ExprSyntax" seems pretty useful. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants