DIP 1027--String Interpolation--Final Review Discussion Thread

Steven Schveighoffer schveiguy at gmail.com
Mon Feb 3 15:17:10 UTC 2020


On 2/3/20 9:52 AM, Adam D. Ruppe wrote:
> On Monday, 3 February 2020 at 14:37:22 UTC, Steven Schveighoffer wrote:
>> I'd do it a little different, so we don't throw away the work the 
>> compiler already did:
>>
>> i"$apples and ${%d}bananas"
>>
>> =>
>>
>> (__d_format_literal!(Format.init, " and ", Format("%d")), apples, 
>> bananas)
> 
> Yes, that would be excellent. If you make a motion to amend the DIP, 
> I'll withdraw my motion and second yours. Let's form a coalition and get 
> Walter onboard!

I hope this can work, but I feel Walter might not be on board due to 
past comments from him. This is why I feel we can wait and get string 
interpolation in, and then later add this.

But I'll throw the idea out there, and see what he says.

> 
> With this, there's no more magic %s from the compiler (though 
> Format.init can and prolly should just return "%s") and there's no more 
> need for % => %% since the library can reliably detect everything.

Yeah, I like that too. For instance mysql can accept i"select * from 
sometable where id = $id" and not have to put the ${?} crap in front of it.

> I'd just note that `Format` here is more realistically `__d_format_item` 
> or something, a new simple thingy rather than a complex phobos struct or 
> whatever.

Right, the naming isn't important.

>> If there is an overload that takes whatever this returns, then this is 
>> used as the lowering. Otherwise, a string literal as specified by the 
>> DIP is used (or we have an alias this in the result to the string 
>> version).
> 
> yeah just alias this it. Let's not put too much magic in there when we 
> already have a library solution with lowering.
> 

I have 2 concerns here. First, I don't want to eagerly construct the 
string if not needed/used. But I can solve this by making toString a 
template function, which enums the constructed string if asked for.

Second concern is that strings in general don't implicitly cast to 
immutable(char)*. Which means printf stops working. Of course, we can 
just enum the format string together, but then it's eagerly constructed.

the "compiler magic" would alleviate both these concerns, which is why I 
suggested it. But if we did the enum with alias this, it would be I 
think fully compatible.

-Steve


More information about the Digitalmars-d mailing list