[M3devel] introducing VAR in more places?
Jay
jayk123 at hotmail.com
Tue Feb 12 15:02:23 CET 2008
C++ fixes this sort of thing. goto around initialization is an error.
C++ constructors and destructors are a really good thing -- can't forgot to initialize or cleanup stuff.
Very much like try/finally, but the finally code is better "positioned".
Rather than special case memory with some fancy garbage collector, provide a mechanism for any type.
RAII and all that.
It's a very good theory at least.
However I've become uncertain of it.
Could be that a huge part of it is ok, if only you could train everyone in RAII and constructors/destructors.
But then there are close pesky fopen/fclose/malloc/free you have to get rid of.
And a lot of complexity around templates, unrelated, but can you allow C++ in a codebase but rigorously ban certain aspects?
Maybe draw an analogy to Itanium.
Lots of obviously good features, not even all that novel. But must we really combine them all together all at once?
(register windows a la SPARC, loop unrolling a la any decent compiler..)
And maybe memory is really vastly more interesting than other resources?Oh, and locks, special case those too...
Mika your switch example, besides being invalid C++, is just not fair.
I don't think people even think to write their code like that.
Introducing variables at the top of a switch is rare, esp. initializing them.
That C allows variables declared anywhere, and open braces anywhere, seems little known and little taken advantage of.
Everyone just moves all the locals to the top, and either programmers do careful static flow analysis, or "lazily" (but reasonably) "over initialize" things, like maybe to 0 or { 0 }.
C++ is what really fixes this properly and people know and understand it.
Declare stuff anywhere, closest to first, use, and preferably initialized or a user defined type with a constructor.
EXCEPT that it doesn't play nicely with goto, and if you aren't using exceptions, then goto is the next best thing...so then you are forced into something closer to the C style, though you gain constructors to help some.
And hopefully the compiler can optimize away a lot of the unnecessary initialization...
Java and I presume C# have a nice feature here too where they require the compiler to error for use of uninitialized variables, though I suspect the specced algorithm misses some things, like:
if (...)
initialize x
common code
if (same thing)
use x <== possible use of uninitialize x, not
At least I've seen other tools (not Java/C# compilers, I don't use those) get confused by this..
- Jay
> To: hendrik at topoi.pooq.com> Date: Tue, 12 Feb 2008 05:41:31 -0800> From: mika at async.caltech.edu> CC: m3devel at elegosoft.com> Subject: Re: [M3devel] introducing VAR in more places?> > hendrik at topoi.pooq.com writes:> >On Mon, Feb 11, 2008 at 10:17:13PM -0600, Rodney M. Bates wrote:> >> I don't think this is what Tony meant. In C++, an opening brace> >> starts a block, but every local declaration in that block has its own> >> unique scope that only starts at that declaration and goes to the end> >> of the block. This is particularly messy because the local declarations> >> of a block need not all precede all the statements,> >> >That is, in my opinion, one of the few things that C and C++ did right, > >well, almost right. It permits a coding style in which every > >variable declaration is initialised, and is declared if and only if it > > You can do all kinds of fun stuff with C's declarations/initializations.> > switch (a) {> int x=7;> case 0:> use(x); /* oops */> ...> }> > goto somewherefun;> > {> int y=7;> > somewherefun:> use(y); /* oops again */> }> > > >has a value. Now that's a useful property. It fails for recursion, > >so there are limits in how far it can apply. And when coding like this, > >you want the simplest syntax to define a constant identifier -- one > >whose value cannot be rebound except by reexecuting the entire block. > >Making something that can change should require more effort -- like > >adding a keyword "var" or some such.> > Sounds to me like you're talking about Modula-3's WITH!> > Mika
_________________________________________________________________
Shed those extra pounds with MSN and The Biggest Loser!
http://biggestloser.msn.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://m3lists.elegosoft.com/pipermail/m3devel/attachments/20080212/6355b03b/attachment-0002.html>
More information about the M3devel
mailing list