freeze and forget it :-)

U

U.Mutlu

What I miss in the language, for ages now, is a keyword
that lets forget/undefine a defined variable:

void f()
{
int x;
...
forget x;
...
}

Such a construct would be useful when analysing/debugging/hand-optimizing
lenghty functions written by others. It helps make code more robust
by undefining the variable as soon as it's not needed anymore.
It also would aid the compiler for optimization.
(Of course one could create the variable in its own scope block,
but that's "cumbersome"/impractical to do for code of others.)

Another construct I miss is: making a variable const sometime later after definition,
ie. promoting it to const by using a keyword "freeze" or such (of course the reverse,
promoting from const to non-const, shall not be possible):

void f()
{
int x;
...
freeze x;
...
}

IMHO it would be very easy and simple to implement these into the language.
OTOH I'm not update with the recent developments of the language,
maybe they already added them? Or is it just wishful thinking? :)
 
8

88888 Dihedral

在 2012å¹´2月12日星期日UTC+8上åˆ11æ—¶55分42秒,U.Mutlu写é“:
What I miss in the language, for ages now, is a keyword
that lets forget/undefine a defined variable:

void f()
{
int x;
...
forget x;
...
}

Such a construct would be useful when analysing/debugging/hand-optimizing
lenghty functions written by others. It helps make code more robust
by undefining the variable as soon as it's not needed anymore.
It also would aid the compiler for optimization.
(Of course one could create the variable in its own scope block,
but that's "cumbersome"/impractical to do for code of others.)
Local variables in confined scopes on a dedicated stack frame was
the old way long tim ago. This trick can be traced back to the
German computer used in world war II.

Allocations of objects in the heap space are not new, too.

But the computing of the reference count and garbage collector to track every object in the heap space passing around in different modules is not standardized in different comupter languages and directly supported by hardware instructions.
Another construct I miss is: making a variable const sometime later afterdefinition,
ie. promoting it to const by using a keyword "freeze" or such (of course the reverse,
promoting from const to non-const, shall not be possible):

void f()
{
int x;
...
freeze x;
...
}

IMHO it would be very easy and simple to implement these into the language.
OTOH I'm not update with the recent developments of the language,
maybe they already added them? Or is it just wishful thinking? :)

The muti-threading management adds overheads in all objects, too.
 
G

Goran

What I miss in the language, for ages now, is a keyword
that lets forget/undefine a defined variable:

void f()
{
   int x;
   ...
   forget x;
   ...

}

Such a construct would be useful when analysing/debugging/hand-optimizing
lenghty functions written by others.

I used macros to that effect. e.g. #define x "can't use x anymore".

But an IDE can do it better. They have all sorts "find uses of..."
commands.

Therefore, I think that similar language feature is a bad idea. It
brings complexity to an already complex language, doesn't simplify the
code either, and code analysis is the job of other tools, not the
language.

Goran.
 
A

Alf P. Steinbach

What I miss in the language, for ages now, is a keyword
that lets forget/undefine a defined variable:

void f()
{
int x;
...
forget x;
...
}

Such a construct would be useful when analysing/debugging/hand-optimizing
lenghty functions written by others. It helps make code more robust
by undefining the variable as soon as it's not needed anymore.
It also would aid the compiler for optimization.
(Of course one could create the variable in its own scope block,
but that's "cumbersome"/impractical to do for code of others.)

If it is cumbersome to add a block, then the code is ungood, perhaps
interleaving the lifetimes of relevant variables. For example, it might
be reusing a variable for different purposes. Needs fixing.

Another construct I miss is: making a variable const sometime later
after definition,
ie. promoting it to const by using a keyword "freeze" or such (of course
the reverse,
promoting from const to non-const, shall not be possible):

void f()
{
int x;
...
freeze x;
...
}

This is again just a block scope, like

for( int i = 1; i <= 10; ++i )
{
int const iOuter = i; auto const i = iOuter;
cout << i << endl;
}

which you can define as a macro if you want:

#define FREEZE( var ) \
auto const& var##Outer; auto const& var = var##Outer

and use as

for( int i = 1; i <= 10; ++i )
{
FREEZE( i );
cout << i << endl;
}

IMHO it would be very easy and simple to implement these into the language.
OTOH I'm not update with the recent developments of the language,
maybe they already added them? Or is it just wishful thinking? :)

Just use braces. ;-)


Cheers & hth.,

- Alf
 
J

Jorgen Grahn

No, it would not make optimization easier; a good optimizer would have
to analyze the whole function anyway.
Why on earth? Cleaning up code is a good thing. Adding any new lines like
'forget x;' would instead clutter it up any more and make the data flow
more complicated, meaning that you are turning the code worse, not
better.

He /does/ have a point though. It's not uncommon to review a function
(perhaps to understand what it does, or look for bugs) and the fewer
variables (less state) to keep track of, the better. In that scenario
you're probably not in a position to rewrite it.

Someone suggested to use an IDE to keep track. What I sometimes like
to do is to /temporarily/ modify the code until it makes sense to both
me and the compiler. And maybe send a patch to the author.

/Jorgen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top