About string and int spliced together.
Adam D. Ruppe
destructionator at gmail.com
Sat Jun 20 13:57:38 UTC 2020
On Saturday, 20 June 2020 at 11:09:12 UTC, aberba wrote:
> So has D gotten so complicated under the hood that its not
> possible to d:
In that proposal, i"" is a string BUILDER, not a string. So it
returns an object you can get a string out of, or do other things
with too.
C# and Javascript also work this way, you just don't notice it as
much due to their different typing systems: the C# object
implicitly converts to string on demand, inserting a hidden call
to its toString method when you assign it like that, and the
Javascript one is passed to a function that does the conversion
with special built-in syntax. JS' foo`$bar` actually calls the
function foo to convert the string builder object to whatever you
want, and if you don't specify a function, the language inserts a
hidden toString function for you automatically.
The DIP there works the same way as those, just without the
hidden function calls. You need to choose what function you want
to call yourself.
// so this works since you call the idup method yourself
// telling it to copy the result of the builder into a
// plain string
string sentence = i"I and ${expression}".idup;
Or you can call other functions and avoid copying for maximum
efficiency and flexibility - no hidden functions to surprise the
low-level D crowd while only needing a very simple function call
if you don't care about that and just want to keep it simple.
Similar to how D's map, filter, etc. may need the extra call to
.array in some use cases. It looks like extra work at first
glace, but it actually enables better code once you get to know
it.
More information about the Digitalmars-d
mailing list