Const spec status.

Bruno Medeiros brunodomedeiros+spam at com.gmail
Sun Apr 27 15:31:17 PDT 2008


This post is *not* another const proposal.

I want to know if something changed with the const system. You see:

Somewhere during the const discussions, I fired up Eclipse (shameless 
publicity) and started testing various code samples around 
const/invariant. This was around const version 3, however, most of the 
code samples I tried didn't work as I expected. It wasn't that I didn't 
grok const, it just seemed the spec had changed somewhat. (and it wasn't 
bug fixes either, as I was following those around, at least the publicly 
know ones, so I would recognized them)
It was if we were now in a version 4 of const (I say version 4 because 
this "new" version seem much better than 3). But I checked the spec:
http://www.digitalmars.com/d/2.0/const3.html
and nothing had changed. Everything seemed to be according to the spec. 
So now I got confused. Maybe Walter's comments in the NG put me offtrack?

To test this theory, *and has an interesting exercise on itself*, I ask 
any participant in the NG who thinks they have a fair understanding of 
const, to consider these simple 6 cases of const usage:

----  ----
module test;

import std.stdio;


struct Struct {
	int x;
}

void main()
{

	// helper objects
	int num = 10;
	invariant(int) inv_num = 1;
	invariant(int[]) inv_intArray = [1, 2];
	invariant(Object) inv_object = cast(invariant) new Object();
	invariant Struct inv_Struct = { 1 };


	/** 6 Const cases **/

	// Case 1
	int a = inv_num;
	
	// Case 2
	invariant(int) b = num;

	// Case 3
	const(int)[] c = inv_intArray;

	// Case 4
	int[] d = inv_intArray;
	
	// Case 5
	Object e = inv_object;
	
	// Case 6
	Struct f = inv_Struct;
	
}

----  ----

and tell which ones you think are valid and which are not. Before 
running them through the compiler of course! (or checking other's 
answers) Just look at the current documentation 
(http://www.digitalmars.com/d/2.0/const3.html), and/or what has been 
said in the newsgroups, to try to figure it out. Also, nothing of this 
has changed with 2.013, it worked the same with at least 2.012.

I think we may see some interesting results.

Also, with the answers to code cases above, one can quickly construct a 
case that (yet again) actually *breaks* the const system.

-- 
Bruno Medeiros - Software Developer, MSc. in CS/E graduate
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D



More information about the Digitalmars-d mailing list