From 8b3be64a53d5f83842b6b259da9e63ba784c39bf Mon Sep 17 00:00:00 2001 From: Jonathan Marler Date: Fri, 7 Dec 2018 08:30:45 -0700 Subject: [PATCH 01/23] Added some links and implementation details --- Drafts/1NNN-JSH.md | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/Drafts/1NNN-JSH.md b/Drafts/1NNN-JSH.md index 3908d2c3d..24a92cb73 100644 --- a/Drafts/1NNN-JSH.md +++ b/Drafts/1NNN-JSH.md @@ -19,6 +19,9 @@ This DIP proposes adding a "tuple/sequence literal" to D, which opens up tons up - https://forum.dlang.org/thread/qpuxtedsiowayrhgyell@forum.dlang.org - https://forum.dlang.org/thread/ncwpezwlgeajdrigegee@forum.dlang.org - https://dlang.typeform.com/report/H1GTak/PY9NhHkcBFG0t6ig (#3 in "What language features do you miss?") +- Exploration: https://github.com/marler8997/interpolated_strings +- Example Library Solution: https://github.com/dlang/phobos/pull/6339/files +- Implementation: https://github.com/dlang/dmd/pull/7988 ## Contents * [Rationale](#rationale) @@ -37,12 +40,30 @@ chances of the DIP being understood and carefully evaluated. ## Description -Required. +Lexer Change: + +Current: + +``` +Token: + ... + StringLiteral + ... +``` +New: + +``` +Token: + ... + StringLiteral + i StringLiteral + ... +``` + +No change to grammar. Implementation consists of a small change to `lex.d` to detect when string literals are prefixed with the `i` character. It adds a boolean flag to string literals to keep track of which ones are "interpolated". Then in the parse stage, if a string literal is marked as "interpolated" then it lowers it to a tuple of strings and expressions. + +Implementation and tests can be found here: https://github.com/dlang/dmd/pull/7988/files -Detailed technical description of the new semantics. Language grammar changes -(per https://dlang.org/spec/grammar.html) needed to support the new syntax -(or change) must be mentioned. Examples demonstrating the new semantics will -strengthen the proposal and should be considered mandatory. ## Breaking Changes and Deprecations None. :) From f9ba6f39ec82ed51bb7bfdfdead06e33c06fedef Mon Sep 17 00:00:00 2001 From: jash11 Date: Fri, 7 Dec 2018 16:08:45 -0500 Subject: [PATCH 02/23] create SI draft --- Drafts/1NNN-JSH.md | 61 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Drafts/1NNN-JSH.md diff --git a/Drafts/1NNN-JSH.md b/Drafts/1NNN-JSH.md new file mode 100644 index 000000000..68eb7b408 --- /dev/null +++ b/Drafts/1NNN-JSH.md @@ -0,0 +1,61 @@ +# String Interpolation + +| Field | Value | +|-----------------|-----------------------------------------------------------------| +| DIP: | (number/id -- assigned by DIP Manager) | +| Review Count: | 0 (edited by DIP Manager) | +| Author: | Jason Hansen | +| Implementation: | https://git.io/fpSUA | +| Status: | Will be set by the DIP manager (e.g. "Approved" or "Rejected") | + +## Abstract + +This DIP proposes adding string interpolation (hereby referred to as SI"). + +### Reference + +- https://forum.dlang.org/thread/khcmbtzhoouszkheqaob@forum.dlang.org +- https://forum.dlang.org/thread/c2q7dt$67t$1@digitaldaemon.com +- https://forum.dlang.org/thread/qpuxtedsiowayrhgyell@forum.dlang.org +- https://forum.dlang.org/thread/ncwpezwlgeajdrigegee@forum.dlang.org +- https://dlang.typeform.com/report/H1GTak/PY9NhHkcBFG0t6ig (#3 in "What language features do you miss?") + +## Contents +* [Rationale](#rationale) +* [Description](#description) +* [Breaking Changes and Deprecations](#breaking-changes-and-deprecations) +* [Copyright & License](#copyright--license) +* [Reviews](#reviews) + +## Rationale + +SI allows writing code that is more concise, readable, and maintanable whenever data needs to be embedded in a string. + +(we should find some examples in the phobos or dmd source that would benefit a lot from SI and put them here) + +A short motivation about the importance and benefits of the proposed change. An existing, +well-known issue or a use case for an existing projects can greatly increase the +chances of the DIP being understood and carefully evaluated. + +## Description + +Required. + +Detailed technical description of the new semantics. Language grammar changes +(per https://dlang.org/spec/grammar.html) needed to support the new syntax +(or change) must be mentioned. Examples demonstrating the new semantics will +strengthen the proposal and should be considered mandatory. + +## Breaking Changes and Deprecations +None. :) + +## Copyright & License + +Copyright (c) 2018 by the D Language Foundation + +Licensed under [Creative Commons Zero 1.0](https://creativecommons.org/publicdomain/zero/1.0/legalcode.txt) + +## Reviews + +The DIP Manager will supplement this section with a summary of each review stage +of the DIP process beyond the Draft Review. From 066b9c9bb569a97e84d7a505c287a85f9ea0dee6 Mon Sep 17 00:00:00 2001 From: jash11 Date: Fri, 7 Dec 2018 16:20:37 -0500 Subject: [PATCH 03/23] update --- Drafts/1NNN-JSH.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Drafts/1NNN-JSH.md b/Drafts/1NNN-JSH.md index 68eb7b408..3908d2c3d 100644 --- a/Drafts/1NNN-JSH.md +++ b/Drafts/1NNN-JSH.md @@ -10,7 +10,7 @@ ## Abstract -This DIP proposes adding string interpolation (hereby referred to as SI"). +This DIP proposes adding a "tuple/sequence literal" to D, which opens up tons up possibities, including string interpolation. ### Reference @@ -29,8 +29,6 @@ This DIP proposes adding string interpolation (hereby referred to as SI"). ## Rationale -SI allows writing code that is more concise, readable, and maintanable whenever data needs to be embedded in a string. - (we should find some examples in the phobos or dmd source that would benefit a lot from SI and put them here) A short motivation about the importance and benefits of the proposed change. An existing, From ad74cdd2bfd68ab7de732c77c0d23b301722ced2 Mon Sep 17 00:00:00 2001 From: jash11 <45018245+jash11@users.noreply.github.com> Date: Fri, 7 Dec 2018 16:58:13 -0500 Subject: [PATCH 04/23] Update 1NNN-JSH.md --- Drafts/1NNN-JSH.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Drafts/1NNN-JSH.md b/Drafts/1NNN-JSH.md index 24a92cb73..1c56da394 100644 --- a/Drafts/1NNN-JSH.md +++ b/Drafts/1NNN-JSH.md @@ -4,7 +4,7 @@ |-----------------|-----------------------------------------------------------------| | DIP: | (number/id -- assigned by DIP Manager) | | Review Count: | 0 (edited by DIP Manager) | -| Author: | Jason Hansen | +| Author: | Jason Hansen, Jonathan Marler | | Implementation: | https://git.io/fpSUA | | Status: | Will be set by the DIP manager (e.g. "Approved" or "Rejected") | From 6c28dba24b69ad6aae3a7fd372dfc99295452373 Mon Sep 17 00:00:00 2001 From: jash11 Date: Fri, 7 Dec 2018 18:00:26 -0500 Subject: [PATCH 05/23] Rephrase "string interpolation" to "string sequence literal" --- Drafts/1NNN-JSH.md | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/Drafts/1NNN-JSH.md b/Drafts/1NNN-JSH.md index 1c56da394..2a4b2b80c 100644 --- a/Drafts/1NNN-JSH.md +++ b/Drafts/1NNN-JSH.md @@ -1,4 +1,4 @@ -# String Interpolation +# String Sequence Literals | Field | Value | |-----------------|-----------------------------------------------------------------| @@ -10,18 +10,18 @@ ## Abstract -This DIP proposes adding a "tuple/sequence literal" to D, which opens up tons up possibities, including string interpolation. +This DIP proposes adding a "string sequence literal"(explain more what this means) to D, which opens up tons up possibilities, including string interpolation. ### Reference +- Exploration: https://github.com/marler8997/interpolated_strings +- Example Library Solution: https://github.com/dlang/phobos/pull/6339/files +- Implementation: https://github.com/dlang/dmd/pull/7988 - https://forum.dlang.org/thread/khcmbtzhoouszkheqaob@forum.dlang.org - https://forum.dlang.org/thread/c2q7dt$67t$1@digitaldaemon.com - https://forum.dlang.org/thread/qpuxtedsiowayrhgyell@forum.dlang.org - https://forum.dlang.org/thread/ncwpezwlgeajdrigegee@forum.dlang.org - https://dlang.typeform.com/report/H1GTak/PY9NhHkcBFG0t6ig (#3 in "What language features do you miss?") -- Exploration: https://github.com/marler8997/interpolated_strings -- Example Library Solution: https://github.com/dlang/phobos/pull/6339/files -- Implementation: https://github.com/dlang/dmd/pull/7988 ## Contents * [Rationale](#rationale) @@ -32,11 +32,33 @@ This DIP proposes adding a "tuple/sequence literal" to D, which opens up tons up ## Rationale -(we should find some examples in the phobos or dmd source that would benefit a lot from SI and put them here) +Sequence literals apply to a wide range of use cases. A few of these use cases are outlined below. + +#### String Interpolation +One notable use for sequence literals is in string interpolation, which allows for more concise, readable, and maintanable code. For example: + + +src/build.d:556:
+`auto hostDMDURL = "http://downloads.dlang.org/releases/2.x/"~hostDMDVer~"/dmd."~hostDMDBase;`
+Becomes:
+`auto hostDMDURL = i"http://downloads.dlang.org/releases/2.x/$hostDMDVer/dmd.$hostDMDBase".text;`
+And, with syntax highlighing:
+![https://i.imgur.com/tXm6rBU.png](https://i.imgur.com/tXm6rBU.png) + + +src/dmd/json.d:1058:
+``s ~= prefix ~ "`" ~ enumName ~ "`";``
+Becomes:
+``s ~= i"prefix`$enumName`".text;``
+With syntax highlighting:
+![https://i.imgur.com/KTcOS0F.png](https://i.imgur.com/KTcOS0F.png) + + +#### Database Queries +TODO: ... + -A short motivation about the importance and benefits of the proposed change. An existing, -well-known issue or a use case for an existing projects can greatly increase the -chances of the DIP being understood and carefully evaluated. +(also, add other use cases) ## Description @@ -66,7 +88,7 @@ Implementation and tests can be found here: https://github.com/dlang/dmd/pull/79 ## Breaking Changes and Deprecations -None. :) +None. :smile: ## Copyright & License From 5ce9b8862c691688c3134567c82cf1a1f598aa6b Mon Sep 17 00:00:00 2001 From: "H. S. Teoh" Date: Fri, 7 Dec 2018 16:03:23 -0800 Subject: [PATCH 06/23] Expand abstract. --- Drafts/1NNN-JSH.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/Drafts/1NNN-JSH.md b/Drafts/1NNN-JSH.md index 2a4b2b80c..a3f8054a1 100644 --- a/Drafts/1NNN-JSH.md +++ b/Drafts/1NNN-JSH.md @@ -10,7 +10,28 @@ ## Abstract -This DIP proposes adding a "string sequence literal"(explain more what this means) to D, which opens up tons up possibilities, including string interpolation. +This DIP proposes adding a "string sequence literal" to D, primarily inspired +by string interpolation, but also applicable to a wide variety of use cases. + +In a nutshell, this literal: + +```` +i"Hello, ${name}! You have logged on ${count} times." +```` + +is translated to the template argument list: + +```` +AliasSeq!("Hello, ", name, "! You have logged on ", count, " times.") +```` + +where `name` and `count` are aliases to symbols in the surrounding scope, +usually local variables. + +Note that the compiler does not perform any string interpolation; it merely +segments the literal into a sequence of strings and aliases. The intent is for +further processing to be done in library code (see below for more detailed +descriptions of possible applications). ### Reference From 261ef1468b96714a0518afe759eb8986908043cb Mon Sep 17 00:00:00 2001 From: jash11 Date: Fri, 7 Dec 2018 19:41:11 -0500 Subject: [PATCH 07/23] add pros/cons of library --- Drafts/1NNN-JSH.md | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/Drafts/1NNN-JSH.md b/Drafts/1NNN-JSH.md index 2a4b2b80c..7332b4dc8 100644 --- a/Drafts/1NNN-JSH.md +++ b/Drafts/1NNN-JSH.md @@ -10,7 +10,7 @@ ## Abstract -This DIP proposes adding a "string sequence literal"(explain more what this means) to D, which opens up tons up possibilities, including string interpolation. +This DIP proposes adding a "string sequence literal" NOTE:(explain more what this means) to D, which opens up tons up possibilities, including string interpolation. ### Reference @@ -26,6 +26,7 @@ This DIP proposes adding a "string sequence literal"(explain more what this mean ## Contents * [Rationale](#rationale) * [Description](#description) +* [Language vs Library Feature](#language-feature-vs-library-feature) * [Breaking Changes and Deprecations](#breaking-changes-and-deprecations) * [Copyright & License](#copyright--license) * [Reviews](#reviews) @@ -58,7 +59,29 @@ With syntax highlighting:
TODO: ... -(also, add other use cases) +NOTE:(also, add other use cases) + + +## Language Feature vs Library Feature + +It has been brought up that this could be done as a library. Here is a breakdown of the pros and cons of a library implementation as opposed to a language implementation: + +:white_check_mark: Library Pros: +- Requires no language changes + +:x: Library Cons: +- Awkward syntax +- Bad performance + + +:white_check_mark: Language Pros: +- High performance +- Nice syntax +- Better integration (IDEs, syntax highlighting, autocompletion) + +:x: Language Cons: + +NOTE:(Should we have pros/cons for both, or only one?) ## Description @@ -88,7 +111,7 @@ Implementation and tests can be found here: https://github.com/dlang/dmd/pull/79 ## Breaking Changes and Deprecations -None. :smile: +None :smile: ## Copyright & License From dd797a21fb096a1e269ea83f3796df152bae108b Mon Sep 17 00:00:00 2001 From: jash11 Date: Fri, 7 Dec 2018 19:44:57 -0500 Subject: [PATCH 08/23] add library cons --- Drafts/1NNN-JSH.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Drafts/1NNN-JSH.md b/Drafts/1NNN-JSH.md index 7332b4dc8..f75a30308 100644 --- a/Drafts/1NNN-JSH.md +++ b/Drafts/1NNN-JSH.md @@ -72,6 +72,8 @@ It has been brought up that this could be done as a library. Here is a breakdown :x: Library Cons: - Awkward syntax - Bad performance +- Depends on a library for a trivial feature +- Cannot be used with betterC :white_check_mark: Language Pros: From f9502f41422b86d85d2de2fa7c2372532c35df88 Mon Sep 17 00:00:00 2001 From: jash11 Date: Fri, 7 Dec 2018 19:54:45 -0500 Subject: [PATCH 09/23] link to rationale --- Drafts/1NNN-JSH.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Drafts/1NNN-JSH.md b/Drafts/1NNN-JSH.md index 387b53cd2..9547ee212 100644 --- a/Drafts/1NNN-JSH.md +++ b/Drafts/1NNN-JSH.md @@ -30,7 +30,7 @@ usually local variables. Note that the compiler does not perform any string interpolation; it merely segments the literal into a sequence of strings and aliases. The intent is for -further processing to be done in library code (see below for more detailed +further processing to be done in library code (see [rationale](#rationale) for more detailed descriptions of possible applications). ### Reference From 800bf97833652ac81c34ecde21ff488377d1e5cf Mon Sep 17 00:00:00 2001 From: jash11 Date: Fri, 7 Dec 2018 20:14:44 -0500 Subject: [PATCH 10/23] move lib vs lang after description --- Drafts/1NNN-JSH.md | 48 ++++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/Drafts/1NNN-JSH.md b/Drafts/1NNN-JSH.md index 9547ee212..b30331dff 100644 --- a/Drafts/1NNN-JSH.md +++ b/Drafts/1NNN-JSH.md @@ -83,29 +83,6 @@ TODO: ... NOTE:(also, add other use cases) -## Language Feature vs Library Feature - -It has been brought up that this could be done as a library. Here is a breakdown of the pros and cons of a library implementation as opposed to a language implementation: - -:white_check_mark: Library Pros: -- Requires no language changes - -:x: Library Cons: -- Awkward syntax -- Bad performance -- Depends on a library for a trivial feature -- Cannot be used with betterC - - -:white_check_mark: Language Pros: -- High performance -- Nice syntax -- Better integration (IDEs, syntax highlighting, autocompletion) - -:x: Language Cons: - -NOTE:(Should we have pros/cons for both, or only one?) - ## Description Lexer Change: @@ -133,6 +110,31 @@ No change to grammar. Implementation consists of a small change to `lex.d` to de Implementation and tests can be found here: https://github.com/dlang/dmd/pull/7988/files +## Language Feature vs Library Feature + +It has been brought up that this could be done as a library. Here is a breakdown of the pros and cons of a library implementation as opposed to a language implementation: + +:white_check_mark: Library Pros: +- Requires no language changes + +:x: Library Cons: +- Awkward syntax +- Bad performance +- Depends on a library for a trivial feature +- Cannot be used with betterC + + +:white_check_mark: Language Pros: +- High performance +- Nice syntax +- Better integration (IDEs, syntax highlighting, autocompletion) + +:x: Language Cons: + +NOTE:(Should we have pros/cons for both, or only one?) +NOTE:(We should explain why the listed pros/cons make language feature a better choice) + + ## Breaking Changes and Deprecations None :smile: From 0d14031e57bc03d338941665bc6d0c9e0e8c285d Mon Sep 17 00:00:00 2001 From: jash11 Date: Fri, 7 Dec 2018 20:18:38 -0500 Subject: [PATCH 11/23] wording --- Drafts/1NNN-JSH.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Drafts/1NNN-JSH.md b/Drafts/1NNN-JSH.md index b30331dff..d0823db91 100644 --- a/Drafts/1NNN-JSH.md +++ b/Drafts/1NNN-JSH.md @@ -30,8 +30,8 @@ usually local variables. Note that the compiler does not perform any string interpolation; it merely segments the literal into a sequence of strings and aliases. The intent is for -further processing to be done in library code (see [rationale](#rationale) for more detailed -descriptions of possible applications). +further processing to be done in library code (see [rationale](#rationale) for a more detailed +description of possible applications). ### Reference From ce8aa9fbf8254a20e3da01e58501f6d3d83d4740 Mon Sep 17 00:00:00 2001 From: Steven Schveighoffer Date: Fri, 7 Dec 2018 21:14:46 -0500 Subject: [PATCH 12/23] Need to support arbitrary expressions --- Drafts/1NNN-JSH.md | 60 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 52 insertions(+), 8 deletions(-) diff --git a/Drafts/1NNN-JSH.md b/Drafts/1NNN-JSH.md index d0823db91..c51e95f49 100644 --- a/Drafts/1NNN-JSH.md +++ b/Drafts/1NNN-JSH.md @@ -19,17 +19,13 @@ In a nutshell, this literal: i"Hello, ${name}! You have logged on ${count} times." ```` -is translated to the template argument list: +is translated to the list: +````D +"Hello, ", name, "! You have logged on ", count, " times." ```` -AliasSeq!("Hello, ", name, "! You have logged on ", count, " times.") -```` - -where `name` and `count` are aliases to symbols in the surrounding scope, -usually local variables. - Note that the compiler does not perform any string interpolation; it merely -segments the literal into a sequence of strings and aliases. The intent is for +segments the literal into a sequence of strings and expressions. The intent is for further processing to be done in library code (see [rationale](#rationale) for a more detailed description of possible applications). @@ -75,6 +71,36 @@ Becomes:
With syntax highlighting:
![https://i.imgur.com/KTcOS0F.png](https://i.imgur.com/KTcOS0F.png) +Note that because string sequence tuples do not actually lower to individual strings, the `text` call (or similar) is required. It may be worth adding a simple function to druntime for concatenating *only* strings to avoid needing `text`. + +#### Aliasing + +Because the compiler emits basically a list of parameters, the string sequence literal does not have a formal type. However, for convenience, if aliased locally, it will become an `AliasSeq` equivalent. + +Example: +```D +string name; +DateTime lastLoggedIn; +alias seq = i"hello, ${name}, I haven't seen you since ${lastLoggedIn}"; +alias seq2 = AliasSeq!("hello, ", name, ", I haven't seen you since ", lastLoggedIn); +assert(seq == seq2); +``` +As usual, if the sequence is passed to compile-time aliases, it will not bind to a single alias (no change in current behavior). + +#### Expressions + +Expressions are not bindable to aliases, unless the expression is evaluatable at compile-time. For the purposes of simplicity, this proposal does not require any new mechanism, but any such mechanism to bind expressions to aliases would benefit this proposal. + +Because of this, arbitrary expressions based on runtime data are allowed only when used in a runtime argument list. + +Example: +```D +int a = 5; +writeln(i"a + 1 is ${a+1}"); // OK, prints "a + 1 is 6" +alias seq = i"a + 1 is ${a+1}"; // Error, cannot read `a` at compile time +``` + +See optional improvements for possible solutions. #### Database Queries TODO: ... @@ -134,6 +160,24 @@ It has been brought up that this could be done as a library. Here is a breakdown NOTE:(Should we have pros/cons for both, or only one?) NOTE:(We should explain why the listed pros/cons make language feature a better choice) +## Optional Improvements + +Because aliasing expressions is not allowed, there is no requirement to support them. However, aliasing expressions would be very helpful to this proposal. + +Example: + +```D +int a; +int b; +alias x = i"a + b is ${a+b}"; +a = 4; +b = 6; +writeln(x); // writes "a + b is 10" +b = 50; +writeln(x); // writes "a + b is 54" +``` + +The concept would be a *new* feature of D, for lack of a better name, called lazy aliases. If syntax is desired, the term `lazy alias` seems like a natural fit. Essentially, it does the same thing as `lazy` parameters, but is an alias, and therefore has no runtime type or function. The expression is simply evaluated whenever used. ## Breaking Changes and Deprecations None :smile: From 7d363af6cdfbf0e22824cb67184266a4ee1c5f82 Mon Sep 17 00:00:00 2001 From: Steven Schveighoffer Date: Fri, 7 Dec 2018 21:38:18 -0500 Subject: [PATCH 13/23] Move new sections to Description --- Drafts/1NNN-JSH.md | 56 +++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/Drafts/1NNN-JSH.md b/Drafts/1NNN-JSH.md index c51e95f49..da52adb48 100644 --- a/Drafts/1NNN-JSH.md +++ b/Drafts/1NNN-JSH.md @@ -73,34 +73,6 @@ With syntax highlighting:
Note that because string sequence tuples do not actually lower to individual strings, the `text` call (or similar) is required. It may be worth adding a simple function to druntime for concatenating *only* strings to avoid needing `text`. -#### Aliasing - -Because the compiler emits basically a list of parameters, the string sequence literal does not have a formal type. However, for convenience, if aliased locally, it will become an `AliasSeq` equivalent. - -Example: -```D -string name; -DateTime lastLoggedIn; -alias seq = i"hello, ${name}, I haven't seen you since ${lastLoggedIn}"; -alias seq2 = AliasSeq!("hello, ", name, ", I haven't seen you since ", lastLoggedIn); -assert(seq == seq2); -``` -As usual, if the sequence is passed to compile-time aliases, it will not bind to a single alias (no change in current behavior). - -#### Expressions - -Expressions are not bindable to aliases, unless the expression is evaluatable at compile-time. For the purposes of simplicity, this proposal does not require any new mechanism, but any such mechanism to bind expressions to aliases would benefit this proposal. - -Because of this, arbitrary expressions based on runtime data are allowed only when used in a runtime argument list. - -Example: -```D -int a = 5; -writeln(i"a + 1 is ${a+1}"); // OK, prints "a + 1 is 6" -alias seq = i"a + 1 is ${a+1}"; // Error, cannot read `a` at compile time -``` - -See optional improvements for possible solutions. #### Database Queries TODO: ... @@ -135,6 +107,34 @@ No change to grammar. Implementation consists of a small change to `lex.d` to de Implementation and tests can be found here: https://github.com/dlang/dmd/pull/7988/files +#### Aliasing + +Because the compiler emits basically a list of parameters, the string sequence literal does not have a formal type. However, for convenience, if aliased locally, it will become an `AliasSeq` equivalent. + +Example: +```D +string name; +DateTime lastLoggedIn; +alias seq = i"hello, ${name}, I haven't seen you since ${lastLoggedIn}"; +alias seq2 = AliasSeq!("hello, ", name, ", I haven't seen you since ", lastLoggedIn); +assert(seq == seq2); +``` +As usual, if the sequence is passed to compile-time aliases, it will not bind to a single alias (no change in current behavior). + +#### Expressions + +Expressions are not bindable to aliases, unless the expression is evaluatable at compile-time. For the purposes of simplicity, this proposal does not require any new mechanism, but any such mechanism to bind expressions to aliases would benefit this proposal. + +Because of this, arbitrary expressions based on runtime data are allowed only when used in a runtime argument list. + +Example: +```D +int a = 5; +writeln(i"a + 1 is ${a+1}"); // OK, prints "a + 1 is 6" +alias seq = i"a + 1 is ${a+1}"; // Error, cannot read `a` at compile time +``` + +See optional improvements for possible solutions. ## Language Feature vs Library Feature From 3569537d20dfd48a2f710e73086aefdc2b7e7687 Mon Sep 17 00:00:00 2001 From: jash11 <45018245+jash11@users.noreply.github.com> Date: Fri, 7 Dec 2018 22:07:43 -0500 Subject: [PATCH 14/23] Change title --- Drafts/1NNN-JSH.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Drafts/1NNN-JSH.md b/Drafts/1NNN-JSH.md index d0823db91..98636c900 100644 --- a/Drafts/1NNN-JSH.md +++ b/Drafts/1NNN-JSH.md @@ -1,4 +1,4 @@ -# String Sequence Literals +# String Syntax for Compile-Time Sequences | Field | Value | |-----------------|-----------------------------------------------------------------| From 88b141c0f1b7f1c457ee6170d26df080d877033d Mon Sep 17 00:00:00 2001 From: jash11 Date: Fri, 7 Dec 2018 22:19:22 -0500 Subject: [PATCH 15/23] move a paragraph to optional improvements --- Drafts/1NNN-JSH.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Drafts/1NNN-JSH.md b/Drafts/1NNN-JSH.md index 1383a746d..c9cbbe47d 100644 --- a/Drafts/1NNN-JSH.md +++ b/Drafts/1NNN-JSH.md @@ -71,7 +71,6 @@ Becomes:
With syntax highlighting:
![https://i.imgur.com/KTcOS0F.png](https://i.imgur.com/KTcOS0F.png) -Note that because string sequence tuples do not actually lower to individual strings, the `text` call (or similar) is required. It may be worth adding a simple function to druntime for concatenating *only* strings to avoid needing `text`. #### Database Queries @@ -179,6 +178,9 @@ writeln(x); // writes "a + b is 54" The concept would be a *new* feature of D, for lack of a better name, called lazy aliases. If syntax is desired, the term `lazy alias` seems like a natural fit. Essentially, it does the same thing as `lazy` parameters, but is an alias, and therefore has no runtime type or function. The expression is simply evaluated whenever used. +
+Because string sequence tuples do not actually lower to individual strings, a call to `std.conv.text` (or similar) is required. It may be worth adding a simple function to druntime for concatenating *only* strings to avoid needing `text`. + ## Breaking Changes and Deprecations None :smile: From 83f98cfff1414c43748e9903c874be28fff74269 Mon Sep 17 00:00:00 2001 From: jash11 Date: Fri, 7 Dec 2018 22:20:06 -0500 Subject: [PATCH 16/23] Optional improvements -> Possible improvements --- Drafts/1NNN-JSH.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Drafts/1NNN-JSH.md b/Drafts/1NNN-JSH.md index c9cbbe47d..aaa9abfc1 100644 --- a/Drafts/1NNN-JSH.md +++ b/Drafts/1NNN-JSH.md @@ -159,7 +159,7 @@ It has been brought up that this could be done as a library. Here is a breakdown NOTE:(Should we have pros/cons for both, or only one?) NOTE:(We should explain why the listed pros/cons make language feature a better choice) -## Optional Improvements +## Possible Improvements Because aliasing expressions is not allowed, there is no requirement to support them. However, aliasing expressions would be very helpful to this proposal. From cfcc96dcc77ada589c0cefab4f6cb91dc7ea3c9b Mon Sep 17 00:00:00 2001 From: Paul Backus Date: Fri, 7 Dec 2018 23:20:28 -0500 Subject: [PATCH 17/23] Remove changes to alias The proposed "lazy alias" feature is unnecessary, and its presence in the DIP distracts readers from the main focus: string sequence literals. To achieve the same results without language changes, it suffices in almost all cases to wrap the string sequence literal in an explicit AliasSeq instantiation, or a call to std.typecons.tuple: alias ctSeq = AliasSeq!(i"hello, {name}"); auto rtSeq = tuple(i"a + b = ${a+b}"); If lazy evaluation is desired, it can be opted-into explicitly using existing language features (lambdas, lazy parameters). --- Drafts/1NNN-JSH.md | 36 ++---------------------------------- 1 file changed, 2 insertions(+), 34 deletions(-) diff --git a/Drafts/1NNN-JSH.md b/Drafts/1NNN-JSH.md index aaa9abfc1..06f384cba 100644 --- a/Drafts/1NNN-JSH.md +++ b/Drafts/1NNN-JSH.md @@ -19,7 +19,7 @@ In a nutshell, this literal: i"Hello, ${name}! You have logged on ${count} times." ```` -is translated to the list: +is translated to the [compile-time sequence](https://dlang.org/articles/ctarguments.html): ````D "Hello, ", name, "! You have logged on ", count, " times." @@ -106,20 +106,6 @@ No change to grammar. Implementation consists of a small change to `lex.d` to de Implementation and tests can be found here: https://github.com/dlang/dmd/pull/7988/files -#### Aliasing - -Because the compiler emits basically a list of parameters, the string sequence literal does not have a formal type. However, for convenience, if aliased locally, it will become an `AliasSeq` equivalent. - -Example: -```D -string name; -DateTime lastLoggedIn; -alias seq = i"hello, ${name}, I haven't seen you since ${lastLoggedIn}"; -alias seq2 = AliasSeq!("hello, ", name, ", I haven't seen you since ", lastLoggedIn); -assert(seq == seq2); -``` -As usual, if the sequence is passed to compile-time aliases, it will not bind to a single alias (no change in current behavior). - #### Expressions Expressions are not bindable to aliases, unless the expression is evaluatable at compile-time. For the purposes of simplicity, this proposal does not require any new mechanism, but any such mechanism to bind expressions to aliases would benefit this proposal. @@ -130,7 +116,7 @@ Example: ```D int a = 5; writeln(i"a + 1 is ${a+1}"); // OK, prints "a + 1 is 6" -alias seq = i"a + 1 is ${a+1}"; // Error, cannot read `a` at compile time +alias seq = AliasSeq!(i"a + 1 is ${a+1})"; // Error, cannot read `a` at compile time ``` See optional improvements for possible solutions. @@ -161,24 +147,6 @@ NOTE:(We should explain why the listed pros/cons make language feature a better ## Possible Improvements -Because aliasing expressions is not allowed, there is no requirement to support them. However, aliasing expressions would be very helpful to this proposal. - -Example: - -```D -int a; -int b; -alias x = i"a + b is ${a+b}"; -a = 4; -b = 6; -writeln(x); // writes "a + b is 10" -b = 50; -writeln(x); // writes "a + b is 54" -``` - -The concept would be a *new* feature of D, for lack of a better name, called lazy aliases. If syntax is desired, the term `lazy alias` seems like a natural fit. Essentially, it does the same thing as `lazy` parameters, but is an alias, and therefore has no runtime type or function. The expression is simply evaluated whenever used. - -
Because string sequence tuples do not actually lower to individual strings, a call to `std.conv.text` (or similar) is required. It may be worth adding a simple function to druntime for concatenating *only* strings to avoid needing `text`. ## Breaking Changes and Deprecations From add9e91f225f01cb27d357b9163e2266ce294040 Mon Sep 17 00:00:00 2001 From: jash11 Date: Sat, 8 Dec 2018 12:01:21 -0500 Subject: [PATCH 18/23] add database example (still needs work) --- Drafts/1NNN-JSH.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Drafts/1NNN-JSH.md b/Drafts/1NNN-JSH.md index 06f384cba..243fb40a0 100644 --- a/Drafts/1NNN-JSH.md +++ b/Drafts/1NNN-JSH.md @@ -4,7 +4,7 @@ |-----------------|-----------------------------------------------------------------| | DIP: | (number/id -- assigned by DIP Manager) | | Review Count: | 0 (edited by DIP Manager) | -| Author: | Jason Hansen, Jonathan Marler | +| Author: | Jason Hansen | | Implementation: | https://git.io/fpSUA | | Status: | Will be set by the DIP manager (e.g. "Approved" or "Rejected") | @@ -74,7 +74,12 @@ With syntax highlighting:
#### Database Queries -TODO: ... +NOTE: Add a couple sentances about why this use case is beneficial + +`db.exec("UPDATE Foo SET a = ?, b = ?, c = ?, d = ? WHERE id = ?", aval, bval, cval, dval, id);`
+Becomes:
+`db.exec(i"UPDATE Foo SET a = $aval, b = $bval, c = $cval, d = $dval WHERE id = $id");` + NOTE:(also, add other use cases) @@ -119,7 +124,7 @@ writeln(i"a + 1 is ${a+1}"); // OK, prints "a + 1 is 6" alias seq = AliasSeq!(i"a + 1 is ${a+1})"; // Error, cannot read `a` at compile time ``` -See optional improvements for possible solutions. +See [Possible Improvements](#possible-improvements) for possible solutions. ## Language Feature vs Library Feature @@ -143,6 +148,7 @@ It has been brought up that this could be done as a library. Here is a breakdown :x: Language Cons: NOTE:(Should we have pros/cons for both, or only one?) +
NOTE:(We should explain why the listed pros/cons make language feature a better choice) ## Possible Improvements From 4e42bfecfc6f51e65d98b531916c807e83fda778 Mon Sep 17 00:00:00 2001 From: jash11 Date: Sat, 8 Dec 2018 12:04:54 -0500 Subject: [PATCH 19/23] keeps terminology consistant --- Drafts/1NNN-JSH.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Drafts/1NNN-JSH.md b/Drafts/1NNN-JSH.md index 243fb40a0..978e3932b 100644 --- a/Drafts/1NNN-JSH.md +++ b/Drafts/1NNN-JSH.md @@ -74,7 +74,7 @@ With syntax highlighting:
#### Database Queries -NOTE: Add a couple sentances about why this use case is beneficial +NOTE: Add a couple sentences about why this use case is beneficial `db.exec("UPDATE Foo SET a = ?, b = ?, c = ?, d = ? WHERE id = ?", aval, bval, cval, dval, id);`
Becomes:
@@ -107,7 +107,7 @@ Token: ... ``` -No change to grammar. Implementation consists of a small change to `lex.d` to detect when string literals are prefixed with the `i` character. It adds a boolean flag to string literals to keep track of which ones are "interpolated". Then in the parse stage, if a string literal is marked as "interpolated" then it lowers it to a tuple of strings and expressions. +No change to grammar. Implementation consists of a small change to `lex.d` to detect when string literals are prefixed with the `i` character. It adds a boolean flag to string literals to keep track of which ones are "interpolated". Then in the parse stage, if a string literal is marked as "interpolated", it lowers it to a sequence of strings and expressions. Implementation and tests can be found here: https://github.com/dlang/dmd/pull/7988/files @@ -153,7 +153,7 @@ NOTE:(We should explain why the listed pros/cons make language feature a better ## Possible Improvements -Because string sequence tuples do not actually lower to individual strings, a call to `std.conv.text` (or similar) is required. It may be worth adding a simple function to druntime for concatenating *only* strings to avoid needing `text`. +Because string sequence literals do not actually lower to individual strings, a call to `std.conv.text` (or similar) is required. It may be worth adding a simple function to druntime for concatenating *only* strings to avoid needing `text`. ## Breaking Changes and Deprecations None :smile: From 553076da4a9b294e99897e4cb38ced4544f0a8eb Mon Sep 17 00:00:00 2001 From: jash11 Date: Sat, 8 Dec 2018 13:16:38 -0500 Subject: [PATCH 20/23] fix example --- Drafts/1NNN-JSH.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Drafts/1NNN-JSH.md b/Drafts/1NNN-JSH.md index 978e3932b..d463b8077 100644 --- a/Drafts/1NNN-JSH.md +++ b/Drafts/1NNN-JSH.md @@ -78,7 +78,7 @@ NOTE: Add a couple sentences about why this use case is beneficial `db.exec("UPDATE Foo SET a = ?, b = ?, c = ?, d = ? WHERE id = ?", aval, bval, cval, dval, id);`
Becomes:
-`db.exec(i"UPDATE Foo SET a = $aval, b = $bval, c = $cval, d = $dval WHERE id = $id");` +`db.exec(i"UPDATE Foo SET a = $(aval), b = $(bval), c = $(cval), d = $(dval) WHERE id = $(id)");` From ad4ebbf65a5bd6c1b9f654ea108a4a63b3a17118 Mon Sep 17 00:00:00 2001 From: jash11 Date: Sat, 8 Dec 2018 13:26:55 -0500 Subject: [PATCH 21/23] spelling --- Drafts/1NNN-JSH.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Drafts/1NNN-JSH.md b/Drafts/1NNN-JSH.md index d463b8077..1afca7ec0 100644 --- a/Drafts/1NNN-JSH.md +++ b/Drafts/1NNN-JSH.md @@ -4,7 +4,7 @@ |-----------------|-----------------------------------------------------------------| | DIP: | (number/id -- assigned by DIP Manager) | | Review Count: | 0 (edited by DIP Manager) | -| Author: | Jason Hansen | +| Author: | (your name and contact data) | | Implementation: | https://git.io/fpSUA | | Status: | Will be set by the DIP manager (e.g. "Approved" or "Rejected") | @@ -53,7 +53,7 @@ description of possible applications). Sequence literals apply to a wide range of use cases. A few of these use cases are outlined below. #### String Interpolation -One notable use for sequence literals is in string interpolation, which allows for more concise, readable, and maintanable code. For example: +One notable use for sequence literals is in string interpolation, which allows for more concise, readable, and maintainable code. For example: src/build.d:556:
From f3ae2a02dbe34e28ca068c5904a02dd5ee5060b5 Mon Sep 17 00:00:00 2001 From: jash11 <45018245+jash11@users.noreply.github.com> Date: Sat, 8 Dec 2018 16:22:09 -0500 Subject: [PATCH 22/23] Remove "expressions" section As @pbackus mentioned in #5, `std.typecons.tuple` can be used as an alternative to support expressions: ``` import std.typecons; auto foo = tuple(i"a+1 is $(a+1)."); writeln(foo.expand); ``` --- Drafts/1NNN-JSH.md | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/Drafts/1NNN-JSH.md b/Drafts/1NNN-JSH.md index 1afca7ec0..d19a40ab6 100644 --- a/Drafts/1NNN-JSH.md +++ b/Drafts/1NNN-JSH.md @@ -111,21 +111,6 @@ No change to grammar. Implementation consists of a small change to `lex.d` to de Implementation and tests can be found here: https://github.com/dlang/dmd/pull/7988/files -#### Expressions - -Expressions are not bindable to aliases, unless the expression is evaluatable at compile-time. For the purposes of simplicity, this proposal does not require any new mechanism, but any such mechanism to bind expressions to aliases would benefit this proposal. - -Because of this, arbitrary expressions based on runtime data are allowed only when used in a runtime argument list. - -Example: -```D -int a = 5; -writeln(i"a + 1 is ${a+1}"); // OK, prints "a + 1 is 6" -alias seq = AliasSeq!(i"a + 1 is ${a+1})"; // Error, cannot read `a` at compile time -``` - -See [Possible Improvements](#possible-improvements) for possible solutions. - ## Language Feature vs Library Feature It has been brought up that this could be done as a library. Here is a breakdown of the pros and cons of a library implementation as opposed to a language implementation: From ebfc529f1b12d5d9b943fab6c2a1bac77c3a6860 Mon Sep 17 00:00:00 2001 From: jash11 <45018245+jash11@users.noreply.github.com> Date: Wed, 23 Jan 2019 17:08:03 -0500 Subject: [PATCH 23/23] Update 1NNN-JSH.md --- Drafts/1NNN-JSH.md | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/Drafts/1NNN-JSH.md b/Drafts/1NNN-JSH.md index d19a40ab6..00245d1bb 100644 --- a/Drafts/1NNN-JSH.md +++ b/Drafts/1NNN-JSH.md @@ -4,7 +4,7 @@ |-----------------|-----------------------------------------------------------------| | DIP: | (number/id -- assigned by DIP Manager) | | Review Count: | 0 (edited by DIP Manager) | -| Author: | (your name and contact data) | +| Author: | Jason Helson | | Implementation: | https://git.io/fpSUA | | Status: | Will be set by the DIP manager (e.g. "Approved" or "Rejected") | @@ -74,17 +74,12 @@ With syntax highlighting:
#### Database Queries -NOTE: Add a couple sentences about why this use case is beneficial `db.exec("UPDATE Foo SET a = ?, b = ?, c = ?, d = ? WHERE id = ?", aval, bval, cval, dval, id);`
Becomes:
`db.exec(i"UPDATE Foo SET a = $(aval), b = $(bval), c = $(cval), d = $(dval) WHERE id = $(id)");` - -NOTE:(also, add other use cases) - - ## Description Lexer Change: @@ -131,14 +126,7 @@ It has been brought up that this could be done as a library. Here is a breakdown - Better integration (IDEs, syntax highlighting, autocompletion) :x: Language Cons: - -NOTE:(Should we have pros/cons for both, or only one?) -
-NOTE:(We should explain why the listed pros/cons make language feature a better choice) - -## Possible Improvements - -Because string sequence literals do not actually lower to individual strings, a call to `std.conv.text` (or similar) is required. It may be worth adding a simple function to druntime for concatenating *only* strings to avoid needing `text`. +- requires a language change ## Breaking Changes and Deprecations None :smile: