April 15, 2015
On Wednesday, 15 April 2015 at 20:44:07 UTC, Ali Çehreli wrote:
> 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

This is great!
I would still call it just put(), close to writeln()'s interface. It shouldn't break any existing and compiling code, right?
April 16, 2015
On 4/15/15 1: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.

Does ~= chain? -- Andrei
April 16, 2015
On Wednesday, 15 April 2015 at 20:59:25 UTC, Steven Schveighoffer wrote:
> 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

With all the excitement about chaining and ufcs, the with statement is often overlooked.
April 16, 2015
Márcio Martins:

> app.put("foo");
> app.put(var);
> app.put("bar");

I'd like put() to accept a lazy range...

Bye,
bearophile
April 16, 2015
On Wednesday, 15 April 2015 at 20:40:04 UTC, Márcio Martins wrote:
> 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.

I think this works:

import std.range : only;
app.put(only("foo", var, "bar"));

http://dlang.org/phobos/std_range.html#.only
April 16, 2015
On Thursday, 16 April 2015 at 03:53:56 UTC, Andrei Alexandrescu wrote:
>> 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.
>
> Does ~= chain? -- Andrei

I'm not sure I understand.

Appender!string app;

app ~= "hello"
    ~= " "
    ~= "kitty";

--> Error: Cannot modify '" "'

Is the order of evaluation not such that this becomes app ~= ("hello" ~= (" " ~= "kitty"))?
April 16, 2015
On Thu, 16 Apr 2015 21:58:43 +0000, Messenger wrote:

> Appender!string app;
> 
> app ~= "hello"
>      ~= " "
>      ~= "kitty";
> 
> --> Error: Cannot modify '" "'
> 
> Is the order of evaluation not such that this becomes app ~= ("hello" ~=
> (" " ~= "kitty"))?

yes, assign is right associative.

April 17, 2015
On Wednesday, 15 April 2015 at 20:44:07 UTC, Ali Çehreli wrote:
> 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");

A hypothetical variadic put method would have the advantage over this in that it could could calculate the total length and preallocate for all arguments in one go.
April 17, 2015
On 16/04/2015 18:25, Nick Treleaven wrote:
> I think this works:
>
> import std.range : only;
> app.put(only("foo", var, "bar"));

OK, that doesn't work, and an array doesn't either (which was my assumption):

  app.put(["foo", var, "bar"]); // NG

If it had, I thought only might be more efficient than chain, but I could be wrong.
1 2
Next ›   Last »