From fc7a031261c8ddb92990320167fc0e4b274a4a63 Mon Sep 17 00:00:00 2001 From: Raymond Zhang Date: Mon, 23 Sep 2024 16:52:51 -0400 Subject: [PATCH] Create 20240923-simple-string-interpolation.md --- .../20240923-simple-string-interpolation.md | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 cadence/20240923-simple-string-interpolation.md diff --git a/cadence/20240923-simple-string-interpolation.md b/cadence/20240923-simple-string-interpolation.md new file mode 100644 index 00000000..afaf3723 --- /dev/null +++ b/cadence/20240923-simple-string-interpolation.md @@ -0,0 +1,93 @@ +--- +status: draft +flip: 288 +authors: Raymond Zhang (raymond.zhang@flowfoundation.org) +sponsor: Supun Setunga (supun.setunga@flowfoundation.org) +updated: 2024-09-23 +--- + +# FLIP 288: Simple String Interpolation + +## Objective + +This FLIP proposes adding support for simple string interpolation limited to identifiers only (no support for full expressions). + +## Motivation + +Currently Cadence has no support for string interpolation. It is convenient for developers to be able to inline variables in strings as opposed to the current solution of applying `concat` repeatedly. + +In general many languages support string interpolation for readability and ease-of-use. + +## User Benefit + +Developers can avoid repeatedly using `concat` to generate strings which will simplify code and readability. + +## Design Proposal + +There are many possible implementations two of which are highlighted below. The main constraint is backward compatibility, existing Cadence 1.0 code cannot be affected. + +### Python f-string syntax +Support the following: +```python +f"value is {value}" +``` +This change is backwards compatible as it introduces a new class of strings. + +### Swift \\() +Support the following: +```swift +"value is \(value)" +``` +This change is backwards compatible because `\(` is not currently a valid escape character. + +### Drawbacks + +None. + +### Alternatives Considered + +String interpolation is preferred over a formatting function (such as `printf`) for the following reasons: + +- Formatting is error prone (e.g. passing a `String` to `%d`) +- There are many languages which moved from traditional formatting to string interpolation (e.g. Python) + +### Performance Implications + +None, non-interpolated strings should not be affected. + +### Dependencies + +None. + +### Engineering Impact + +This change should be simple to implement. + +### Best Practices + +It may be preferred to use string interpolation over concat once implemented. + +### Compatibility + +Proposed changes are backwards compatible. + +### User Impact + +This is a feature addition, no impact. + +## Related Issues + +Extension to support expressions as opposed to just identifiers. Support custom `toString()` functions as well. + +## Prior Art + +- https://docs.swift.org/swift-book/documentation/the-swift-programming-language/stringsandcharacters/#String-Interpolation +- https://docs.python.org/3/tutorial/inputoutput.html#formatted-string-literals + +## Questions and Discussion + +Feel free to discuss on the PR. + +Questions: +- Preferences or concerns on proposed syntax +- It may be less work to allow expressions versus enforcing identifier only, how much extra testing would be involved?