swift-syntax icon indicating copy to clipboard operation
swift-syntax copied to clipboard

Adds explicit overload for `ExpressibleByStringLiteral` syntax types

Open stevapple opened this issue 3 years ago • 5 comments

Description

Nowadays we have DeclSyntax, ExprSyntax, StmtSyntax, TokenSyntax, TypeSyntax... with ExpressibleByStringLiteral conformance. If an initializer takes a concrete-typed parameter of one of these types, string literal syntax works perfectly, reducing lots of boilerplate codes.

However, for initializers with generic parameters like <E: ExprSyntaxProtocol>, such syntax won't work and we need to explicitly express out ExprSyntax("...") or "..." as ExprSyntax. Adding an overload with concrete type ExprSyntax can solve the problem, making SwiftSyntaxBuilder easier to use.

stevapple avatar Apr 08 '23 11:04 stevapple

rdar://107787687

ahoppen avatar Apr 08 '23 12:04 ahoppen

We have talked about this before and at some point decided that the overhead of duplicating every initializer was not worth the API improvement because in my opinion having to write ExprSyntax("...") is not toooo bad.

I am happy to re-consider this but there always needs to be a balance between the size of SwiftSyntax’s API surface and the improvements they provide for clients.

ahoppen avatar Apr 11 '23 21:04 ahoppen

@ahoppen Do you think something like .expr(…), .text(…), .source(…)… would be slightly better?

stevapple avatar Apr 12 '23 19:04 stevapple

I don’t think that’s would be a huge improvement. It would be yet another paradigm a adopter needs to learn with very marginal improvement in terms of characters.

ahoppen avatar Apr 12 '23 20:04 ahoppen

I opened up a discussion to see if there’s some way to resolve the problem. I don’t mind ExprSyntax(…) either, but allowing literal somewhere and disallowing somewhere else looks weird and makes source breaking easier.

stevapple avatar Apr 12 '23 20:04 stevapple