www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - schwartzSort with a const key

reply "bearophile" <bearophileHUGS lycos.com> writes:
Currently this code doesn't work, because the B array is const, 
so B[i] is const:


import std.algorithm: schwartzSort;
void main() {
     auto A = [0, 1, 2];
     const B = [10, -20, 30];
     schwartzSort!(i => B[i])(A);
}


dmd gives:

...\dmd2\src\phobos\std\conv.d(3734): Error: static assert  
"Don't know how to emplace a const(int) with a const(int)."
...\dmd2\src\phobos\std\algorithm.d(9210):        instantiated 
from here: emplace!(const(int), const(int))
test.d(5):        instantiated from here: 
schwartzSort!(__lambda2, "a < b", cast(SwapStrategy)0, int[])


But the array A is mutable. So is it acceptable for me to ask in 
Bugzilla this code to work?

Bye,
bearophile
Sep 03 2013
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 09/03/2013 03:22 PM, bearophile wrote:

 Currently this code doesn't work, because the B array is const, so B[i]
 is const:


 import std.algorithm: schwartzSort;
 void main() {
      auto A = [0, 1, 2];
      const B = [10, -20, 30];
      schwartzSort!(i => B[i])(A);
The return type of the lambda is deduced as const(int). I think that is silly. A copied value type should not preserve the const attribute. (I vaguely remember discussions on this topic.) Explicit return type works: schwartzSort!(delegate int (i) => B[i])(A); Playing along with the compiler and casting away const also works: schwartzSort!(i => cast()B[i])(A);
 }


 dmd gives:

 ...\dmd2\src\phobos\std\conv.d(3734): Error: static assert "Don't know
 how to emplace a const(int) with a const(int)."
 ...\dmd2\src\phobos\std\algorithm.d(9210):        instantiated from
 here: emplace!(const(int), const(int))
 test.d(5):        instantiated from here: schwartzSort!(__lambda2, "a <
 b", cast(SwapStrategy)0, int[])


 But the array A is mutable. So is it acceptable for me to ask in
 Bugzilla this code to work?
I would think so but I think this issue may already be in bugzilla.
 Bye,
 bearophile
Thanks, Ali
Sep 03 2013
parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Tue, Sep 03, 2013 at 03:57:09PM -0700, Ali Çehreli wrote:
 On 09/03/2013 03:22 PM, bearophile wrote:
 
 Currently this code doesn't work, because the B array is const, so B[i]
 is const:


 import std.algorithm: schwartzSort;
 void main() {
      auto A = [0, 1, 2];
      const B = [10, -20, 30];
      schwartzSort!(i => B[i])(A);
The return type of the lambda is deduced as const(int). I think that is silly. A copied value type should not preserve the const attribute. (I vaguely remember discussions on this topic.) Explicit return type works: schwartzSort!(delegate int (i) => B[i])(A); Playing along with the compiler and casting away const also works: schwartzSort!(i => cast()B[i])(A);
 }


 dmd gives:

 ...\dmd2\src\phobos\std\conv.d(3734): Error: static assert "Don't know
 how to emplace a const(int) with a const(int)."
 ...\dmd2\src\phobos\std\algorithm.d(9210):        instantiated from
 here: emplace!(const(int), const(int))
 test.d(5):        instantiated from here: schwartzSort!(__lambda2, "a <
 b", cast(SwapStrategy)0, int[])


 But the array A is mutable. So is it acceptable for me to ask in
 Bugzilla this code to work?
I would think so but I think this issue may already be in bugzilla.
[...] I filed a new issue just in case: http://d.puremagic.com/issues/show_bug.cgi?id=10960 T -- "Outlook not so good." That magic 8-ball knows everything! I'll ask about Exchange Server next. -- (Stolen from the net)
Sep 03 2013
parent "bearophile" <bearophileHUGS lycos.com> writes:
H. S. Teoh:

 I filed a new issue just in case:

 	http://d.puremagic.com/issues/show_bug.cgi?id=10960
Thank you. But the idea of stripping away the const from copied values misses my main point, so I have added a note there. Bye, bearophile
Sep 03 2013