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

[Code] should be string interpolated to the code string #57064

Closed
cedvdb opened this issue Nov 11, 2024 · 3 comments
Closed

[Code] should be string interpolated to the code string #57064

cedvdb opened this issue Nov 11, 2024 · 3 comments
Labels
area-pkg Used for miscellaneous pkg/ packages not associated with specific area- teams. feature-macros Implementation of the macros feature pkg-macros The experimental package:_macros library type-enhancement A request for a change that isn't a bug

Comments

@cedvdb
Copy link
Contributor

cedvdb commented Nov 11, 2024

Currently using Code in an interpolated String will result in Instance of.. . In the context of macros, a more sensible option would be to override Code.toString() so string interpolatation returns a string that represents the actual code.

Use case:

Using strings is more readable than arrays in some cases. I wanted to do

RawCode.fromString('$valueReference.toIso8601String()')

but had to do:

RawCode.fromParts([valueReference, '.toIso8601String()']);

In place like here https://github.com/dart-lang/sdk/pull/57063/files#diff-e10def922b15028782e7fdcf395c5acab6f0cc2e86c1696a3e936eb775396d7aR638

@cedvdb cedvdb changed the title [Code] should be interpollable as a String [Code] should override toString Nov 11, 2024
@cedvdb cedvdb changed the title [Code] should override toString [Code] override toString Nov 11, 2024
@dart-github-bot
Copy link
Collaborator

Summary: The user wants to use a string instead of an array to represent code in the RawCode class, but the current API only accepts an array of parts. They suggest using a fromString constructor for better readability.

@dart-github-bot dart-github-bot added area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-enhancement A request for a change that isn't a bug labels Nov 11, 2024
@cedvdb cedvdb changed the title [Code] override toString [Code] should be string interpolated to the code string Nov 11, 2024
@devoncarew devoncarew added the feature-macros Implementation of the macros feature label Nov 11, 2024
@devoncarew devoncarew added area-pkg Used for miscellaneous pkg/ packages not associated with specific area- teams. pkg-macros The experimental package:_macros library and removed area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. labels Nov 11, 2024
@lrhn
Copy link
Member

lrhn commented Nov 11, 2024

The reason you cannot use a string interpolation is that it will eagerly create string at that point, and the macro processor doesn't know whether the reference will need an import prefix, or what it will be, until it has seen all the macro generated code.
Passing the reference as an object allows the macro processor to recognize that a reference is made, and allows it to generate the best possible code for that reference at a later time, when more information is available.

@cedvdb
Copy link
Contributor Author

cedvdb commented Nov 12, 2024

I see. Maybe there is the possibility for a string lookalike, such as Code.fromCodeString(c'$code.doSomething') where c'...' would not be a string but it would look like one. Internally something like

factory Code.fromCodeString(CodeString stringLookalike) {
  return Code.fromParts(stringLookalike.toParts());
}

This may have been suggested elsewhere so I will close this as I just wanted to give my unimformed two cents.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-pkg Used for miscellaneous pkg/ packages not associated with specific area- teams. feature-macros Implementation of the macros feature pkg-macros The experimental package:_macros library type-enhancement A request for a change that isn't a bug
Projects
Development

No branches or pull requests

4 participants