After a recent discussion on this list about tail-const class references, it became rather clear that someone interested would have to implement the thing if we were to have it. So I did it. See enhancement request 5325 for an explanation and a patch.
<http://d.puremagic.com/issues/show_bug.cgi?id=5325>

Let's hope Walter likes my patch.

In the tail-const thread, I proposed the challenge of making this code work when an array of const object is passed as an argument:

        T[] giveMeASortedArray(alias Predicate, T)(T[] t) {
                // creating new array of the same length but with assignable 
elements
                auto copy = new Unqual!(typeof(t[0]))[t.length];
                foreach (index, value; t)
                        copy[index] = value;
        
                // sorting the copy
                sort!(Predicate)(copy);
                return copy;
        }

Well, with the patch I made it now works!... irrespective of the type attribute (const,immutable,shared,inout). The only modification required to Phobos is to make to!string() compile when passed a const(Object) because somehow 'sort' requires that.

Here is how the function is invoked:

        void main() {
                int*[] a = giveMeASortedArray!("a < b")(new int*[12]);
                Object[] b = giveMeASortedArray!("a < b")(new Object[12]);
        
                const(int*)[] c = giveMeASortedArray!("a < b")(new 
const(int*)[12]);
const(Object)[] d = giveMeASortedArray!("cast(void*)a < cast(void*)b")(new const(Object)[12]);
        }

See the strange predicate for the const(Object) version? That's because opCmp() in Object doesn't work with const.

Two minor modifications are required in Phobos to make the above compile. First, to!string(const(Object)) needs an implementation because somehow 'sort' requires it. Also template std.traits.isMutable needed an adjustment so that swap()'s template constrains are satisfied.


--
Michel Fortin
michel.for...@michelf.com
http://michelf.com/

Reply via email to