| Thread overview | |||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
April 15, 2015 How about appender.put() with var args? | ||||
|---|---|---|---|---|
| ||||
Hi!
I use Appender a lot, and find it ugly to write this all the time to efficiently construct strings:
app.put("foo");
app.put(var);
app.put("bar");
How about this instead?
app.put("foo", var, "bar");
This would be consistent with the writeln interface, and it could also reduce the syntax overhead of using appenders vs string concats.
Was this overlooked or is there some issue I am not seeing? :)
Cheers,
-M
| ||||
April 15, 2015 Re: How about appender.put() with var args? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Márcio Martins | On 4/15/15 3:09 PM, "=?UTF-8?B?Ik3DoXJjaW8=?= Martins\" <marcioapm@gmail.com>\"" wrote:
> Hi!
>
> I use Appender a lot, and find it ugly to write this all the time to
> efficiently construct strings:
>
> app.put("foo");
> app.put(var);
> app.put("bar");
>
> How about this instead?
>
> app.put("foo", var, "bar");
>
> This would be consistent with the writeln interface, and it could also
> reduce the syntax overhead of using appenders vs string concats.
>
> Was this overlooked or is there some issue I am not seeing? :)
>
> Cheers,
> -M
What about:
import std.format;
app.formattedWrite("foo%sbar", var);
-Steve
| |||
April 15, 2015 Re: How about appender.put() with var args? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Márcio Martins | Appender will take a range, so you can also do:
app.put(["foo", var, "bar"]);
or
app.put(chain("foo", var, "bar"));
But yes, a variadic put would be convenient so long as it wasn't ambiguous in some way.
| |||
April 15, 2015 Re: How about appender.put() with var args? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Wednesday, 15 April 2015 at 19:16:55 UTC, Steven Schveighoffer wrote:
> On 4/15/15 3:09 PM, "=?UTF-8?B?Ik3DoXJjaW8=?= Martins\" <marcioapm@gmail.com>\"" wrote:
>> Hi!
>>
>> I use Appender a lot, and find it ugly to write this all the time to
>> efficiently construct strings:
>>
>> app.put("foo");
>> app.put(var);
>> app.put("bar");
>>
>> How about this instead?
>>
>> app.put("foo", var, "bar");
>>
>> This would be consistent with the writeln interface, and it could also
>> reduce the syntax overhead of using appenders vs string concats.
>>
>> Was this overlooked or is there some issue I am not seeing? :)
>>
>> Cheers,
>> -M
>
> What about:
>
> import std.format;
> app.formattedWrite("foo%sbar", var);
>
> -Steve
Well, but wouldn't that incur the cost of parsing the format
string, for no benefit?
| |||
April 15, 2015 Re: How about appender.put() with var args? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Justin Whear | On Wednesday, 15 April 2015 at 19:41:13 UTC, Justin Whear wrote:
> Appender will take a range, so you can also do:
> app.put(["foo", var, "bar"]);
>
> or
> app.put(chain("foo", var, "bar"));
>
> But yes, a variadic put would be convenient so long as it wasn't
> ambiguous in some way.
I guess chain could work in some cases, and are not that bad on the aesthetic side, I supposed. However having to include std.range just for that, and more importantly, all parameters having to be the same type, as opposed to just being "appendable".
Creating an array inline is also not an option as I generally don't like to trade aesthetics <=> efficiency.
| |||
April 15, 2015 Re: How about appender.put() with var args? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Márcio Martins | On 04/15/2015 12:09 PM, "=?UTF-8?B?Ik3DoXJjaW8=?= Martins\" <marcioapm@gmail.com>\"" wrote:
> Hi!
>
> I use Appender a lot, and find it ugly to write this all the time to
> efficiently construct strings:
>
> app.put("foo");
> app.put(var);
> app.put("bar");
>
> How about this instead?
>
> app.put("foo", var, "bar");
Agreed.
If a different name like putAll() is acceptable:
void putAll(A, T...)(A a, T items)
{
foreach (item; items){
a.put(item);
}
}
// ...
app.putAll("foo", var, "bar");
Ali
| |||
April 15, 2015 Re: How about appender.put() with var args? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Márcio Martins | On Wednesday, 15 April 2015 at 19:09:42 UTC, Márcio Martins wrote:
> Hi!
>
> I use Appender a lot, and find it ugly to write this all the time to efficiently construct strings:
>
> app.put("foo");
> app.put(var);
> app.put("bar");
>
Sidetracking a bit, but when I started using Appender I was surprised to see that put didn't return a reference to the Appender itself. Had it done so, you could have chained your put calls very nicely.
app.put("foo")
.put(var)
.put("bar")
.put(more)
.put("stuff");
You can naturally write a small wrapper function that does this for you, but it still strikes me as odd. Sadly I imagine changing the return type would make the function signature mangle differently, breaking ABI compatibility.
| |||
April 15, 2015 Re: How about appender.put() with var args? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Márcio Martins | On 4/15/15 4:34 PM, "=?UTF-8?B?Ik3DoXJjaW8=?= Martins\" <marcioapm@gmail.com>\"" wrote: > On Wednesday, 15 April 2015 at 19:16:55 UTC, Steven Schveighoffer wrote: >> What about: >> >> import std.format; >> app.formattedWrite("foo%sbar", var); > > Well, but wouldn't that incur the cost of parsing the format > string, for no benefit? The benefit is, you are not limited to strings :P But yeah, if it's just strings, you have not much benefit. However, I don't know what the cost is for parsing compared to the rest, it may not be too significant. I like Ali's idea, make it a general purpose "putAll" primitive, goes right in std.range.primitives. -Steve | |||
April 15, 2015 Re: How about appender.put() with var args? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Messenger | On 4/15/15 4:51 PM, Messenger wrote:
> On Wednesday, 15 April 2015 at 19:09:42 UTC, Márcio Martins wrote:
>> Hi!
>>
>> I use Appender a lot, and find it ugly to write this all the time to
>> efficiently construct strings:
>>
>> app.put("foo");
>> app.put(var);
>> app.put("bar");
>>
>
> Sidetracking a bit, but when I started using Appender I was surprised to
> see that put didn't return a reference to the Appender itself. Had it
> done so, you could have chained your put calls very nicely.
>
> app.put("foo")
> .put(var)
> .put("bar")
> .put(more)
> .put("stuff");
>
> You can naturally write a small wrapper function that does this for you,
> but it still strikes me as odd. Sadly I imagine changing the return type
> would make the function signature mangle differently, breaking ABI
> compatibility.
with(app)
{
put(var);
put("bar");
put(more);
put("stuff");
}
-Steve
| |||
April 15, 2015 Re: How about appender.put() with var args? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Wednesday, 15 April 2015 at 20:59:25 UTC, Steven Schveighoffer wrote:
> with(app)
> {
> put(var);
> put("bar");
> put(more);
> put("stuff");
> }
>
> -Steve
Awesome.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply