Const sucks
Bruno Medeiros
brunodomedeiros+spam at com.gmail
Tue Sep 11 03:45:15 PDT 2007
Walter Bright wrote:
> Bruno Medeiros wrote:
>> Walter Bright wrote:
>>> What, exactly, is the use case that needs a solution?
>>
>> Here's another use case, which is significantly different than the
>> other (there should be lots more to be found). Say you have a Person
>> class, which has a name, and for whatever reasons you want the name be
>> a String class instead of const(char)[]. Then you have:
>>
>> class Person {
>> const(String) name;
>> ...
>>
>> but now how do you change the name of the Person?...
>
> I'd redesign String so it holds invariant data, then just have:
>
> class Person {
> String name;
> ...
>
Err... you do realize what this implies? This example holds not just for
String but for the general case where one would want to use aggregation
of immutable objects. In all those cases you'd have to redesign the
aggregated class to be immutable, with all the annoyances that has. That
basicly amounts to not be able to use const/invariant for classes, and
then in that regard D would revert to the same status quo as Java and
other languages.
(Another alternative is to use an actual pointer to a class reference,
but that's sure to bring another slew of inconveniences)
--
Bruno Medeiros - MSc in CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
More information about the Digitalmars-d
mailing list