any news on const/invariant?
Rioshin an'Harthen
rharth75 at hotmail.com
Wed Nov 28 08:59:55 PST 2007
"Derek Parnell" <derek at psych.ward> kirjoitti viestissä
news:1vst9y1mnh6kz.tkg1phefemws$.dlg at 40tude.net...
> My point is that any help that a compiler can give to the coder in
> preventing coding errors is a worthwhile pursuit. Sure, there are
> diminishing returns and maybe this is one of them. I'm not sure because I
> have not analyzed any empirical statistics.
>
> I am simply saying, and this seems to be very hard to get across, that if
> there is some syntax to express the design concept of "the pointer may not
> be modified but the items pointed to can be", then the compiler can detect
> at compile time when the coder attempts to change the pointer, and issue
> the appropriate warning.
>
> A compiler exists to make life easier for the coder.
>
> If this truely is a case where "Head const turned out to be an
> unexplainable, incomprehensible feature." and/or is too hard to implement,
> then I'll go with the flow. I haven't seen evidence of that being the case
> yet, regardless of Walter's statements so far.
I believe head const is a feature that should exist. I'm just a bit unsure
of how it should be integrated with the new version of const, since the main
thing, IMO, is that it should be as simple as possible.
We can, I think, basically separate constness into four categories (I'm not
talking about invariants, just head and tail const):
1: no constness
2: tail constness
3: head constness
4: head and tail constness
The syntax for cases 1 and 2 are now getting quite clear (I'll just use
"ref" here in these examples):
1: void foo(ref byte[] bar)
2: void foo(ref const(byte[]) bar) - at least I think it would be this
way... :)
The cases for 3 and 4 could just basically be an extension of this, with the
"ref" keyword basically required to mark head constness as follows:
3: void foo(const(ref) byte[] bar)
4: void foo(const(ref) const(byte[]) bar)
I admit these cases looks ugly, but at least they tell exactly what is going
on: the reference itself is constant, and in case four the type, as well.
(As a side-note, I was at first considering the following syntax:
3: void foo(const(ref byte[]) bar)
4: void foo(const(ref const(byte[])) bar)
but I believe the last case to be even more uglier than in the variant above
and case 3 to be ambivalent: is the intent to make the reference constant,
or the reference and the byte[] itself?)
More information about the Digitalmars-d
mailing list