D's greatest mistakes

Michel Fortin michel.fortin at michelf.com
Tue Nov 30 11:22:31 PST 2010


On 2010-11-30 10:36:53 -0500, Andrei Alexandrescu 
<SeeWebsiteForEmail at erdani.org> said:

> On 11/29/10 7:52 PM, Michel Fortin wrote:
>> Now consider this generic code that uses Unqual:
>> 
>> 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];
>> 
>> // using a loop because copy[] = t[] doesn't enforce const properly!
>> foreach (index, value; t)
>> copy[index] = value;
>> 
>> // sorting the copy
>> sort!(Predicate)(copy);
>> return copy;
>> }
>> 
>> The above algorithm will work for types like const(int*), but it breaks
>> if T is const(C) and C is a class. So it seems we'll need a special
>> cases for generic algorithms to work for classes. That's what I meant
>> when I say Rebindable is a fragile solution. It works in most cases, but
>> it breaks in some others, mostly when you want to be generic.
>> 
>> So here's the challenge for you Andrei:
> [snip]
> 
> I agree that the problem is difficult but disagree with the angle. This 
> is not the challenge, and it is not only mine to take. To the extent 
> we're interested in making D a successful language, we're all on the 
> same boat, so the challenge belongs to us all.

The challenge is to make Rebindable, Unqual, and const() play well with 
generic programming. If you can do that plainly as a library solution, 
then great. If not, then the library solution is poor. I don't care if 
someone else than you find the solution, but right now you're the one 
pushing for Rebindable so I directed the challenge at you.

Since you don't seem ready to analyzing the problem at hand, here's a 
few hints. There's basically two serious blockers in it (in addition to 
multiple smaller issues):

1. Unqual!(const C) would need to become Rebindable!(const C)
2. Rebindable!(const C)[] would need to be implicitly castable to const(C)[].

Number one is easy to fix, although it might interfere with the 
intended meaning of Unqual. Number two is the real semantic problem... 
how can an array of struct be implicitly cast to an array of class 
references?

Also, if you find a solution for arrays, it should apply to other 
containers too. List!(Rebindable!(const C)) should be implicitly 
castable to List!(const(C)) and also const(List!(C)).

Implementing this is already hard when const is always a type modifier. 
If constness is sometime expressed as a type modifier and sometime 
expressed as some kind of smart reference struct, it can only become 
more difficult. Don't you agree?


-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/



More information about the Digitalmars-d mailing list