const(Object)ref is here!

Graham St Jack Graham.StJack at internode.on.net
Sun Dec 5 20:46:27 PST 2010


First, I have to say that it is wonderful that someone is taking a 
serious look at this area again, and even better, you have come up with 
a compiler patch to make it happen!

Some questions (assuming your patch or something like it gets into dmd):

Does this mean that I would be able to write this:
immutable(Foo)ref foo; // create a reference
foo = new immutable(Foo)(); // re-bind it (not sure about "new" syntax 
for immutables)

Are there any other show-stopping syntax issues that are holding up 
widespread adoption/rollout of const-correctness?

What do Walter and Andrei think?

Does anyone know what the plan is for rolling const-correctness 
thoughout druntime and phobos (and shared/immutable in std.concurrency)? 
Having to do casting all the time is a real drag. I for one would be 
happy to help if help is needed.


On 06/12/10 11:21, Michel Fortin wrote:
> 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.
>
>


-- 
Graham St Jack



More information about the Digitalmars-d mailing list