Jump to page: 1 2
Thread overview
Lazy formatting
Feb 01, 2021
Der
Feb 02, 2021
Paolo Invernizzi
Feb 02, 2021
Paolo Invernizzi
Feb 02, 2021
Paolo Invernizzi
Feb 03, 2021
Paolo Invernizzi
Feb 03, 2021
Paolo Invernizzi
Feb 01, 2021
Kagamin
Feb 01, 2021
Adam D. Ruppe
Feb 01, 2021
rikki cattermole
Feb 01, 2021
Kagamin
Feb 01, 2021
Vladimir Panteleev
Feb 02, 2021
Berni44
January 31, 2021
As a response to Andrei in DIP 1036 thread (as this is somewhat off topic):

> On 1/31/21 11:11 AM, Steven Schveighoffer wrote:
>> This is how I would do it:
>>
>> i"Hello, ${var.withFormat!"%45s"} world";
>>
>> (where withFormat provides a toString overload that works with idup). No extra intermediate allocations necessary.
> 
> I wonder if we can define a nice convention by the use of past tense vs. active.
> 
> format!"%45s"(val) -> format using this spec the following values
> 
> val.formatted!"%45s" -> value formatted with this spec
> 
> We have just a bit of that with sort/sorted.
> 
> Maybe flesh that up in phobos 2.0 :o).
> 

I have often wanted a lazy formatting feature where you aren't allocating but it returns a string-able thing that can be used as an input range. I was surprised that phobos doesn't provide this.

Would be really awesome to have, but I think possibly it means we need to reengineer the lower level. Probably a phobos 2.0 thing.

-Steve
February 01, 2021
On Monday, 1 February 2021 at 00:25:42 UTC, Steven Schveighoffer wrote:
> Probably a phobos 2.0 thing.

Is this under serious consideration, or is it merely the optimists' fervent dream.
January 31, 2021
On 1/31/21 7:43 PM, Der wrote:
> On Monday, 1 February 2021 at 00:25:42 UTC, Steven Schveighoffer wrote:
>> Probably a phobos 2.0 thing.
> 
> Is this under serious consideration, or is it merely the optimists' fervent dream.

Walter, Andrei, Atila and I are discussing for about 4 months. I have only been involved since then, not sure what the plans were before that.

This is not a facetious thing. We are trying to determine the best way to create a phobos v2 that does not break existing code. I don't know what it will look like, or if we can come up with something that is workable soon. But we are working on it. The thing that I'm most interested in is no autodecoding. But of course, with a place where we can create breaking changes that live in an "opt-in" space, it opens the door to a lot of other things that we may want to include.

-Steve
February 01, 2021
> This is how I would do it:
>
> i"Hello, ${var.withFormat!"%45s"} world";

I have a feeling recent DIPs are too detached from practice. If you read feature requests, people wanted interpolated strings in order to easily construct strings without tinkering. For ease of use. But the DIP's design rationale ended up being "because other languages have this". As design thus loses direction you end up designing an abstract silver bullet of interpolated strings without regard for practical needs. But we already have plenty of ways to tinker with string formatting. Who asked for one more way to do it? Almost all uses of interpolated strings are trivial ones i"I have ${n} apples".
February 01, 2021
On Monday, 1 February 2021 at 13:31:48 UTC, Kagamin wrote:
>> i"Hello, ${var.withFormat!"%45s"} world";
>
> I have a feeling recent DIPs are too detached from practice.
> [...]
> Who asked for one more way to do it? Almost all uses of interpolated strings are trivial ones i"I have ${n} apples".

In ruby and php and stuff you can do

"I have ${some_function()} apples".

That's what withFormat is, simply a function you're calling in there which does a format.

The DIP itself just allows for

i"I have ${d_code_here} apples."


and d_code_here can be a simple variable or it can be a function call or other fancy things. All the same feature.
February 02, 2021
On 02/02/2021 2:31 AM, Kagamin wrote:
> Almost all uses of interpolated strings are trivial ones i"I have ${n} apples".

SQL is a good example which is not trivial due to anti-injection techniques like escaping.
February 01, 2021
On Monday, 1 February 2021 at 13:40:22 UTC, rikki cattermole wrote:
> On 02/02/2021 2:31 AM, Kagamin wrote:
>> Almost all uses of interpolated strings are trivial ones i"I have ${n} apples".
>
> SQL is a good example which is not trivial due to anti-injection techniques like escaping.

Such queries usually appear in tests, where you work with trusted data. In any case %45s means nothing for sql, and you can't use anything like that anyway.
February 01, 2021
On Monday, 1 February 2021 at 00:25:42 UTC, Steven Schveighoffer wrote:
> I have often wanted a lazy formatting feature where you aren't allocating but it returns a string-able thing that can be used as an input range. I was surprised that phobos doesn't provide this.
>
> Would be really awesome to have, but I think possibly it means we need to reengineer the lower level. Probably a phobos 2.0 thing.

Um, do you mean something like this?

https://github.com/CyberShadow/ae/blob/904d5b6ab7a15b3ecac499ef41bb40a88fd64e1e/utils/text/package.d#L47

assert(64.formatted!"%x".text == "40");

It's not an input range (I think emitting formatted strings as an input range without an intermediary buffer would be impractically difficult in general), but it does use the sink toString interface.
February 01, 2021
On 2/1/21 8:31 AM, Kagamin wrote:
>> This is how I would do it:
>>
>> i"Hello, ${var.withFormat!"%45s"} world";
> 
> I have a feeling recent DIPs are too detached from practice. If you read feature requests, people wanted interpolated strings in order to easily construct strings without tinkering. For ease of use. But the DIP's design rationale ended up being "because other languages have this". As design thus loses direction you end up designing an abstract silver bullet of interpolated strings without regard for practical needs. But we already have plenty of ways to tinker with string formatting. Who asked for one more way to do it? Almost all uses of interpolated strings are trivial ones i"I have ${n} apples".

I'm not sure how you read this as "cuz other langs do it": https://github.com/dlang/DIPs/blob/344e00ee2d6683d61ee019d5ef6c1a0646570093/DIPs/DIP1036.md#rationale

But in any case, the `withFormat` thing has nothing to do with the referenced DIP. That's why we're in a separate thread here.

I have wanted in the past to just have a construct that says "format this value", but not have to decide where it goes. In other words, a lazy formatting.

All of std.format expects a *destination* to put formatted data. I want the *source* to just be formatted data.

It's actually a lower level abstraction than what is already in std.format, which could be built upon such a thing. Just an idea any way (and of course, it's relevant for string interpolation to anyone who might want a one-off format difference than the default).

-Steve
February 01, 2021
On 2/1/21 10:28 AM, Vladimir Panteleev wrote:
> On Monday, 1 February 2021 at 00:25:42 UTC, Steven Schveighoffer wrote:
>> I have often wanted a lazy formatting feature where you aren't allocating but it returns a string-able thing that can be used as an input range. I was surprised that phobos doesn't provide this.
>>
>> Would be really awesome to have, but I think possibly it means we need to reengineer the lower level. Probably a phobos 2.0 thing.
> 
> Um, do you mean something like this?
> 
> https://github.com/CyberShadow/ae/blob/904d5b6ab7a15b3ecac499ef41bb40a88fd64e1e/utils/text/package.d#L47 
> 
> 
> assert(64.formatted!"%x".text == "40");
> 
> It's not an input range (I think emitting formatted strings as an input range without an intermediary buffer would be impractically difficult in general), but it does use the sink toString interface.

Oh nice, yes! That would actually work with the DIP as defined.

Yeah, you definitely need a buffer for formatting, it's somewhat possible to do with integers, but not as pleasant.

But local buffers can be part of the type anyway (like a static array).

-Steve
« First   ‹ Prev
1 2