DMD 2.000 alpha release
Walter Bright
newshound1 at digitalmars.com
Thu Jun 21 00:30:08 PDT 2007
Derek Parnell wrote:
> On Wed, 20 Jun 2007 19:55:47 -0700, Walter Bright wrote:
>
>> Charles D Hixson wrote:
>>> The potential argument in favor of const strings seems, to me, to be
>>> speed. But I don't know whether it applies.
>> The argument for const strings isn't speed, it's understandability.
>
> I thought it was about safety. About not accidentally changing stuff that
> was not indented to be changed.
The two are closely related.
>> and by making
>> strings const (or invariant) it makes them behave like value types.
>
> This is where you lose me though. Your statement seems to only apply when
> talking about function parameters -- and then it really is about speed too.
>
> For example, I can get the same effect as const even when not using const,
> but at a run-time cost.
>
> Without CONST
> void func(char[] a) { ... }
> char str = "abc".dup;
> func(str.dup); // Now I don't care what 'func' does with my data.
>
> With CONST
> void func(const (char)[] a) { ... }
> char str = "abc".dup;
> func(str); // I still don't care what 'func' does with my data.
>
> The difference is that the CONST version is faster and gives the compiler a
> clue about the writer's intentions (and thus can issue appropriate error
> messages)
Value types work by making lots of copies. By making a reference type
const, it can behave as a value type without the cost of making copies.
In that context, yes, it is about speed.
More information about the Digitalmars-d-announce
mailing list