digitalmars.D.learn - How to add a character literal to a string without ~ operator?
- BoQsc (18/18) Apr 04 I'm looking for more readable standard function to add a
- Salih Dincer (13/22) Apr 04 ```d
- Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= (11/30) Apr 04 My favorite d feature is lazy ranges. No allocation here.
- user1234 (14/53) Apr 04 ```d
- Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= (4/21) Apr 04 I don't understand your point sorry. I didn't imply anything
- Salih Dincer (13/18) Apr 04 ```d
- IchorDev (10/23) Apr 08 Concatenate is the verb you're looking for, not add. 'Adding' a
I'm looking for more readable standard function to add a **character** literal to a **string**. The `~` operator is clearly not great while reading a source code. I'm not here to discuss that. I'm looking for a function inside standard library. The function should be straightforward, up to two words. Here is what I expect from a programming language: Pseudo example: ``` import std; void main(){ string word = hello; join(word, 'f', " ", "World"); writeln(word); // output: hellof World } ```
Apr 04
On Thursday, 4 April 2024 at 18:14:54 UTC, BoQsc wrote:I'm looking for more readable standard function to add a **character** literal to a **string**. The `~` operator is clearly not great while reading a source code. I'm not here to discuss that. I'm looking for a function inside standard library. The function should be straightforward, up to two words. Here is what I expect from a programming language: Pseudo example:```d import std.array, std.stdio; void main() { auto word = appender("hello"); word.put('f'); word.put(" "); word.put("World"); word.writeln; } ``` SDB 79
Apr 04
On Thursday, 4 April 2024 at 18:14:54 UTC, BoQsc wrote:I'm looking for more readable standard function to add a **character** literal to a **string**. The `~` operator is clearly not great while reading a source code. I'm not here to discuss that. I'm looking for a function inside standard library. The function should be straightforward, up to two words. Here is what I expect from a programming language: Pseudo example: ``` import std; void main(){ string word = hello; join(word, 'f', " ", "World"); writeln(word); // output: hellof World } ```My favorite d feature is lazy ranges. No allocation here. ``` auto s = chain("as ", "df ", "j"); // s is lazy writeln(s); ``` Of course you can allocate a new string from the chained range: ``` string str = cast(string)s.array.assumeUnique; // without a cast it is a dstring (why though?) ```
Apr 04
On Thursday, 4 April 2024 at 19:56:50 UTC, Ferhat Kurtulmuş wrote:On Thursday, 4 April 2024 at 18:14:54 UTC, BoQsc wrote:```d module runnable; import std.stdio : writeln; import std.range : chain; void main() nogc { auto s = chain("as ", "df ", "j"); // s is lazy writeln(s); } ``` Bad example. The range is indeed a ` nogc` lazy input range but `writeln` is not a ` nogc` consumer.I'm looking for more readable standard function to add a **character** literal to a **string**. The `~` operator is clearly not great while reading a source code. I'm not here to discuss that. I'm looking for a function inside standard library. The function should be straightforward, up to two words. Here is what I expect from a programming language: Pseudo example: ``` import std; void main(){ string word = hello; join(word, 'f', " ", "World"); writeln(word); // output: hellof World } ```My favorite d feature is lazy ranges. No allocation here. ``` auto s = chain("as ", "df ", "j"); // s is lazy writeln(s); ``` Of course you can allocate a new string from the chained range: ``` string str = cast(string)s.array.assumeUnique; // without a cast it is a dstring (why though?) ```/tmp/temp_7F91F8531AB0.d(9,12): Error: ` nogc` function `D main` cannot call non- nogc function `std.stdio.writeln!(Result).writeln` /bin/ldc2-/bin/../import/std/stdio.d(4292,6): which calls `std.stdio.trustedStdout`The input range consumer has to be nogc as well.
Apr 04
On Thursday, 4 April 2024 at 21:23:00 UTC, user1234 wrote:On Thursday, 4 April 2024 at 19:56:50 UTC, Ferhat Kurtulmuş wrote:I don't understand your point sorry. I didn't imply anything about nogc. I of course know writeln is not nogc. I just kept the example simple.[...]```d module runnable; import std.stdio : writeln; import std.range : chain; void main() nogc { auto s = chain("as ", "df ", "j"); // s is lazy writeln(s); } ``` Bad example. The range is indeed a ` nogc` lazy input range but `writeln` is not a ` nogc` consumer.[...]The input range consumer has to be nogc as well.
Apr 04
On Thursday, 4 April 2024 at 19:56:50 UTC, Ferhat Kurtulmuş wrote:My favorite d feature is lazy ranges. No allocation here. ```d auto s = chain("as ", "df ", "j"); // s is lazy writeln(s); ``````d import std.range : chain; void main() { string word = "hello"; auto noError = chain(word, "f", " ", "World"); /** auto noCompile = chain('f', " ", "World"); * ^ *************************|****************/ } ``` SDB 79
Apr 04
On Thursday, 4 April 2024 at 18:14:54 UTC, BoQsc wrote:I'm looking for more readable standard function to add a **character** literal to a **string**.Concatenate is the verb you're looking for, not add. 'Adding' a `char` to a `string` sounds like you want `myString[] += myChar;`, which wouldn't compile because `string`s are aliases of `immutable(char)[]`.Pseudo example: ``` import std; void main(){ string word = hello; join(word, 'f', " ", "World"); writeln(word); // output: hellof World } ```I'd usually use [`text`](https://dlang.org/phobos/std_conv.html#text). It automatically converts each parameter to a string and concatenates all of them. If you prefer format strings, there's [`format`](https://dlang.org/phobos/std_format.html#format).
Apr 08