From 6045905002546086519
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: f78e5,fbe10986a90d9275,start
X-Google-Attributes: gidf78e5,public
X-Google-ArrivalTime: 2004-01-19 08:07:44 PST
Path: archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!kibo.news.demon.net!mutlu.news.demon.net!demon!mail2news.demon.co.uk!devnull
From: omarshaikh71@yahoo.com (Omar Shaikh)
Newsgroups: comp.std.c++
Subject: Proposal for adding two keywords for explicit modification
Date: Mon, 19 Jan 2004 16:07:42 +0000 (UTC)
Lines: 113
Approved: fjh@cs.mu.oz.au (Fergus Henderson , moderator of comp.std.c++)
Message-ID: <20040119114736.63476.qmail@web20904.mail.yahoo.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: quoted-printable
X-Trace: mail2news.demon.co.uk 1074528462 10406 10.0.0.1 (19 Jan 2004 16:07:42 GMT)
X-Complaints-To: abuse@demon.net
NNTP-Posting-Date: Mon, 19 Jan 2004 16:07:42 +0000 (UTC)
X-Received: from mulga.cs.mu.oz.au ([128.250.1.22])
	by news.demon.co.uk with esmtp (Exim 4.12)
	id 1AibwD-0002hY-00
	for mail2news@news.news.demon.net; Mon, 19 Jan 2004 16:07:42 +0000
X-Received: from mulga.cs.mu.OZ.AU (localhost [127.0.0.1]) by mulga.cs.mu.OZ.AU with ESMTP
	id i0JG7dmC023916; Tue, 20 Jan 2004 03:07:39 +1100 (EST)
X-Received: (from fjh@localhost)
	by mulga.cs.mu.OZ.AU (8.12.10+Sun/8.12.9/Submit) id i0JG7dbC023907;
	Tue, 20 Jan 2004 03:07:39 +1100 (EST)
X-Authentication-Warning: mulga.cs.mu.OZ.AU: fjh set sender to devnull@stump.algebra.com using -f
X-Path: comp-std-cpp-robomod!not-for-mail
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
X-Delivered-To: std-c++@ucar.edu
X-Spam-Level: 
X-Spam-Status: No, hits=1.0 required=5.2 tests=FROM_ENDS_IN_NUMS autolearn=no 
	version=2.60-mulga_r1
X-Spam-Checker-Version: SpamAssassin 2.60-mulga_r1 (1.212-2003-09-23-exp) on 
	mulga.cs.mu.OZ.AU
X-Newsgroups: comp.std.c++
X-MIME-Autoconverted: from 8bit to quoted-printable by mulga.cs.mu.OZ.AU id i0JG7dmC023916
Xref: archiver1.google.com comp.std.c++:889

Hello, all

I have a suggestion to make that I believe should be a
very useful addition to the C++ programming language.
Please do let me know if what I have to suggest makes
sense, and any suggestion would be definitely welcome.
The whole purpose of this addition to the language is
to prevent _accidental_ over-writing of a value, since
this facility allows one to over-write only explicitly
(explicit modification). I have faced the need for a
feature of this sort many times; thus this suggestion.

Sometimes, a constant should be modifiable, though
this may sound like a contradiction. A const is too
"stiff," while a variable is too unsafe.  A middle
path is, therefore, called for. I would like to
suggest two keywords: freeze and thaw, which should
fill in that need. Consider the following example to
see how these two keywords work.

main ()
{
	freeze int i =3D 10;	// Declaring a "frozen" variable,
=93freeze int i at 10=94
	freeze j =3D 20;		// Implicitly an int
	freeze int k(30);	// Constructor syntax also works

	// what won't work
	freeze int m;	// ERROR -> Freeze m at what value?
Initialization is necessary!
	double arr[i];	// ERROR -> A freezable variable is
NOT a constant,
			// so cannot be used for array declarations

	i =3D 40;		// ERROR -> i is "frozen,=94 you cannot change
it!
	thaw i =3D 50;	// Ok to "thaw" a frozen variable and
immediately change it
	i =3D 60;		// Oops! i is frozen again!
}

We could simulate this behaviour in a class using
methods to "thaw" the frozen value. This method,
however, turns out to be very cumbersome since
overloaded operators would have to be defined not just
for every conceivable operation, but also for all the
primitive types in the language. Secondly, any attempt
to modify a frozen value should immediately give a
syntax error. In the case of a class, however, only a
run-time exception would be possible, defeating the
concept of early error detection.

Another feature of a freezable is that it should be
thawable only within its own block or any underlying
block. Outside its own block, its reference can only
be read into a "more severe" type like a const. Take
the following example:

class Foo
{
	freezable int x;
public:
	Foo () : x(10)
	{}

	freezable int& GetRef () {
		return x;
	}

	void Changex (int);
};

int main ()
{
	Foo f;
	int& i =3D f.GetRef();		// Error! Cannot read into a
modifiable variable.
	freezable int& j =3D f.GetRef();	// Error! Not enough
"severity."
	const int& k =3D f.GetRef();	// Works fine since k is a
const
}

void Foo::Changex (int new_val)
{
	// ok to thaw and change a frozen here since
	// this block is within x's declaration block
	thaw x =3D new_val;
}

The reason that a freezable reference can only be read
into a const is that freezable data - for all=20
practical purposes - is a constant to outsiders, and
is meant to be thawed and modified only by its=20
owner.

I would appreciate any further suggestions / remarks
on this.

Thank you
Omar

__________________________________
Do you Yahoo!?
Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes
http://hotjobs.sweepstakes.yahoo.com/signingbonus

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]



