| |
![user1234's Gravatar profile Gravatar of user1234](//www.gravatar.com/avatar/2f5aac196e5a94948d3a54580e9c4a37?d=identicon&s=32) | Posted by user1234 in reply to Ferhat Kurtulmuş | Permalink Reply |
|
user1234 ![user1234's Gravatar profile Gravatar of user1234](//www.gravatar.com/avatar/2f5aac196e5a94948d3a54580e9c4a37?d=identicon&s=80)
Posted in reply to Ferhat Kurtulmuş
| 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:
> 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?)
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.
> /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.
|