D const design rationale
Bruno Medeiros
brunodomedeiros+spam at com.gmail
Sat Jun 23 10:22:41 PDT 2007
Sean Kelly wrote:
>
>>> I can appreciate that 'invariant' may be of tremendous use to the
>>> compiler, but I balk at the notion of adding language features that
>>> seem largely intended as compiler "hints."
>>
>> It's a lot more than that. First, there's the self-documenting aspect
>> of it. Second, it opens the way for functional programming, which can
>> be of huge importance.
>
> Could you explain? I've been trying to determine how I would write a
> library using these new features, and so far, 'invariant' simply seems
> more an obstacle to maintainability (because of the necessary code
> duplication), than it is an aid to programming.
>
In which situation does having 'invariant' lead to code duplication? I
can see the common situation where you have class getter methods, or
other similiar methods, but even without 'invariant' and only with const
and non-const you have that problem.
>
>>> The compiler can inspect the code however, and a global const is as
>>> good as an invariant for optimization (as far as I know). As for the
>>> rest, I think the majority of remaining cases aren't ones where
>>> 'invariant' would apply anyway: dynamic buffers whose contents are
>>> guaranteed not to change either in word or by design, etc.
>>
>> But they do apply - that's the whole array optimization thing. You're
>> not just going to have global arrays.
>
> Perhaps I misunderstood the "must be known at compile-time" clause. Can
> 'invariant' apply to dynamic arrays that will remain unchanged once
> initialized? In my work I use almost no static data--it's all generated
> on the fly or loaded from some data source. Will 'invariant' help to
> make my code more optimal?
>
>
> Sean
Of course, that's the whole point of invariant. The 'invariant' keyword
is being used for two things, compile-time values, and invariant data.
You obviously can't do what you mention with compile-time values, but
you can with invariant data (which is created at runtime). (or have I
misunderstood you, and you already knew that?)
--
Bruno Medeiros - MSc in CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
More information about the Digitalmars-d
mailing list