March 08, 2012
On Thursday, March 08, 2012 10:21:48 Matthias Walter wrote:
> Also there is no usable swap() method of Array. So do I really have to perform the swap on my own? I mean, 3 lines of code aren't that much but I really expected an easy way.

There's std.algorithm.swap if you want a swap function.

- Jonathan M Davis
March 08, 2012
On Thursday, March 08, 2012 10:21:48 Matthias Walter wrote:
>but the following did not work:
>
>std.algorithm.swap(arrayInstance[i], arrayInstance[j]);

What error did you get exactly? Since that exact call should work. "It didn't work" doesn't help.

--
James Miller
March 08, 2012
On 03/08/2012 10:48 AM, James Miller wrote:
> On Thursday, March 08, 2012 10:21:48 Matthias Walter wrote:
>> but the following did not work:
>>
>> std.algorithm.swap(arrayInstance[i], arrayInstance[j]);
> 
> What error did you get exactly? Since that exact call should work. "It didn't work" doesn't help.

Of course - sorry for the imprecise description:


import std.container;
import std.algorithm;

int main(char[][] args)
{
  auto c = Array!(double)(cast(double[]) []);
  c.insertBack(1.0);
  c.insertBack(2.0);
  std.algorithm.swap(c[0], c[1]);

  return 0;
}

yields:

main.d(9): Error: template std.algorithm.swap(T) if (isMutable!(T) &&
!is(typeof(T.init.proxySwap(T.init)))) does not match any function
template declaration
main.d(9): Error: template std.algorithm.swap(T) if (isMutable!(T) &&
!is(typeof(T.init.proxySwap(T.init)))) cannot deduce template function
from argument types !()(double,double)


Best regards,

Matthias
March 08, 2012
On 8 March 2012 23:16, Matthias Walter <xammy@xammy.homelinux.net> wrote:
> On 03/08/2012 10:48 AM, James Miller wrote:
>> On Thursday, March 08, 2012 10:21:48 Matthias Walter wrote:
>>> but the following did not work:
>>>
>>> std.algorithm.swap(arrayInstance[i], arrayInstance[j]);
>>
>> What error did you get exactly? Since that exact call should work. "It didn't work" doesn't help.
>
> Of course - sorry for the imprecise description:
>
>
> import std.container;
> import std.algorithm;
>
> int main(char[][] args)
> {
>  auto c = Array!(double)(cast(double[]) []);
>  c.insertBack(1.0);
>  c.insertBack(2.0);
>  std.algorithm.swap(c[0], c[1]);
>
>  return 0;
> }
>
> yields:
>
> main.d(9): Error: template std.algorithm.swap(T) if (isMutable!(T) &&
> !is(typeof(T.init.proxySwap(T.init)))) does not match any function
> template declaration
> main.d(9): Error: template std.algorithm.swap(T) if (isMutable!(T) &&
> !is(typeof(T.init.proxySwap(T.init)))) cannot deduce template function
> from argument types !()(double,double)
>
>
> Best regards,
>
> Matthias

According to this double is not mutable, which makes sense, I think it might be because opIndex on Array doesn't return a reference.

This seems like a bug to me, however its probably better to implement the swap for Array in array, due to weirdness with returning refs for operator overloads. File a bug report on it.

--
James Miller
Top | Discussion index | About this forum | D home