November 01, 2014 Re: pop & popFront combined | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On Saturday, 1 November 2014 at 13:22:34 UTC, Nordlöw wrote: > On Saturday, 1 November 2014 at 11:45:25 UTC, Nordlöw wrote: >> So why isn't something like x.stealFront already in Phobos? > > First try here: > > https://github.com/nordlow/justd/blob/master/range_ex.d#L14 > > Please comment! That is: > auto ref stealFront(R)(ref R r) > { > import std.range: moveFront, popFront; > auto e = r.moveFront; > r.popFront; > return e; > } `auto ref` is nonsense here. You can't return a reference to `e` as it's a local variable. |
November 01, 2014 Re: pop & popFront combined | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On Saturday, 1 November 2014 at 13:25:03 UTC, Nordlöw wrote:
> On Saturday, 1 November 2014 at 13:22:34 UTC, Nordlöw wrote:
>> https://github.com/nordlow/justd/blob/master/range_ex.d#L14
>>
>> Please comment!
>
> What's the recommended way of making stealFront and stealBack inout here? Can I somehow use auto ref together with inout?
I don't see what you'd need inout for here. stealFront/stealBack are not methods.
|
November 01, 2014 Re: pop & popFront combined | ||||
---|---|---|---|---|
| ||||
Posted in reply to anonymous | On Saturday, 1 November 2014 at 13:30:16 UTC, anonymous wrote:
> On Saturday, 1 November 2014 at 13:22:34 UTC, Nordlöw wrote:
>> On Saturday, 1 November 2014 at 11:45:25 UTC, Nordlöw wrote:
>>> So why isn't something like x.stealFront already in Phobos?
>>
>> First try here:
>>
>> https://github.com/nordlow/justd/blob/master/range_ex.d#L14
>>
>> Please comment!
>
> That is:
>
>> auto ref stealFront(R)(ref R r)
>> {
>> import std.range: moveFront, popFront;
>> auto e = r.moveFront;
>> r.popFront;
>> return e;
>> }
>
> `auto ref` is nonsense here. You can't return a reference to `e`
> as
> it's a local variable.
It's probably intended to mean `auto` + `ref`, not `auto ref`. `ref` alone is already sufficient to get type deduction.
|
November 01, 2014 Re: pop & popFront combined | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On Saturday, 1 November 2014 at 13:25:03 UTC, Nordlöw wrote:
> On Saturday, 1 November 2014 at 13:22:34 UTC, Nordlöw wrote:
>> https://github.com/nordlow/justd/blob/master/range_ex.d#L14
>>
>> Please comment!
>
> What's the recommended way of making stealFront and stealBack inout here? Can I somehow use auto ref together with inout?
If you want to avoid the temporary variable, you could write:
scope(success) r.popFront;
return r.moveFront;
|
November 01, 2014 Re: pop & popFront combined | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz | On Saturday, 1 November 2014 at 13:36:05 UTC, Marc Schütz wrote: > On Saturday, 1 November 2014 at 13:30:16 UTC, anonymous wrote: [...] >>> auto ref stealFront(R)(ref R r) >>> { >>> import std.range: moveFront, popFront; >>> auto e = r.moveFront; >>> r.popFront; >>> return e; >>> } [...] > It's probably intended to mean `auto` + `ref`, not `auto ref`. `ref` alone is already sufficient to get type deduction. But ref is wrong. The function returns a local. Just auto would be fine. |
November 01, 2014 Re: pop & popFront combined | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz | On Saturday, 1 November 2014 at 13:38:22 UTC, Marc Schütz wrote:
> If you want to avoid the temporary variable, you could write:
>
> scope(success) r.popFront;
> return r.moveFront;
Does this solution cost performance?
|
November 01, 2014 Re: pop & popFront combined | ||||
---|---|---|---|---|
| ||||
Posted in reply to anonymous | On Saturday, 1 November 2014 at 13:30:16 UTC, anonymous wrote:
> `auto ref` is nonsense here. You can't return a reference to `e`
> as
> it's a local variable.
My mistake. Thanks.
|
November 01, 2014 Re: pop & popFront combined | ||||
---|---|---|---|---|
| ||||
Posted in reply to anonymous | On Saturday, 1 November 2014 at 13:36:05 UTC, anonymous wrote: > I don't see what you'd need inout for here. stealFront/stealBack are not methods. inout can be used on free functions aswell. See for example https://github.com/nordlow/justd/blob/master/algorithm_ex.d#L674 |
November 01, 2014 Re: pop & popFront combined | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz | On Saturday, 1 November 2014 at 13:38:22 UTC, Marc Schütz wrote:
> If you want to avoid the temporary variable, you could write:
>
> scope(success) r.popFront;
> return r.moveFront;
Nice solution anyhow! Thanks!
|
November 01, 2014 Re: pop & popFront combined | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On Saturday, 1 November 2014 at 13:54:31 UTC, Nordlöw wrote:
> On Saturday, 1 November 2014 at 13:38:22 UTC, Marc Schütz wrote:
>> If you want to avoid the temporary variable, you could write:
>>
>> scope(success) r.popFront;
>> return r.moveFront;
>
> Does this solution cost performance?
I guess we have to look at the assembler output to be sure. Is there a convenient way to do this in LDC?
|
Copyright © 1999-2021 by the D Language Foundation