Page 1 of 2
When (x==0) do_something();
Posted: Tue Mar 18, 2008 9:47 pm
by piranha
I just had a thought:
Wouldn't is be nice to have a 'when' keyword?
Used like:
Code: Select all
when (x==0)
{
do_something();
do_anotherthing(arg);
}
You put that at the beginning of your procedure, or where ever, and when x becomes 0 those 2 functions are executed.
-JL
Posted: Tue Mar 18, 2008 10:03 pm
by Brynet-Inc
Seriously?
Code: Select all
#ifdef lazy
#define when(x) if(x)
#elif lazier
#define when(x) while(x)
#endif
Show an example where this "when" keyword would be useful?
Posted: Wed Mar 19, 2008 12:27 am
by piranha
no, your not using it right!!!!
When as in you specify it once, and it becomes constant throughout the function:
Code: Select all
int x;
when(x==0)
{
do_something();
}
x=procedure1();
x=procedure2();
/* at some point x becomes 0 *
* At that point do_something(); is called. *
* That way you don't need to specify *
* that if statement after every function call */
One place? Automatic dying:
1) The when call is made for (x==0)
2) 'x=procedure();'. If it fails, it returns 0. Then when(x==0) is satisfied and the program exits.
-JL
Posted: Wed Mar 19, 2008 1:09 am
by Solar
I see where you are coming from, syntactically. But it would be a nightmare.
INTERCAL, a programming language designed for incomprehensability, has a "COME FROM" construct. What you are suggesting here is a conditional "COME FROM anywhere".
When should the condition be tested for? After each statement? At each execution point, even?
Changing variables in such a construct... the implied ban on optimization wherever such a "when ()" is in effect...
And at the end of the day it isn't even that useful IMHO.
Posted: Wed Mar 19, 2008 1:34 am
by piranha
I know, it would make angry program and compiler.
It could be used for debugging though
-JL
Posted: Wed Mar 19, 2008 1:42 am
by JoeKayzA
Looks somewhat like an event/callback thingy to me, this could be implemented with no problems in c++ (in fact, it's done quite often already). But if you are really after error checking in the first place - what's the big deal with adding a condition check after each function invokation? If you are lazy in writing (which is absolutely ok), use the preprocessor.
Posted: Wed Mar 19, 2008 2:15 am
by Solar
piranha wrote:It could be used for debugging though
You mean a
break condition as supported by any half-decent debugger without having to muck up the sourcecode...?
Posted: Wed Mar 19, 2008 3:18 am
by 01000101
how about making 'x' a global variable and monitor its value through the timer interrupt procedure?
Code: Select all
int timer_val = 0;
int x = 1;
void bleh();
void timer_handler()
{
timer_val++;
if(x == 0){bleh();}
}
void bleh()
{
printf("YAY!");
}
Posted: Wed Mar 19, 2008 3:47 am
by JamesM
01000101 wrote:how about making 'x' a global variable and monitor its value through the timer interrupt procedure?
Code: Select all
int timer_val = 0;
int x = 1;
void bleh();
void timer_handler()
{
timer_val++;
if(x == 0){bleh();}
}
void bleh()
{
printf("YAY!");
}
*COUGH* Atomicity! *COUGH!*
Posted: Wed Mar 19, 2008 3:56 am
by Solar
01000101 wrote:how about making 'x' a global variable and monitor its value through the timer interrupt procedure?
The Complicator's Gloves.
Ask yourself: Which problem would be solved by this construct, and is it worth the hassle?
Posted: Wed Mar 19, 2008 5:40 am
by Combuster
In aspectJ I could create joinpoints around writes to x, then consequently check from there.
In all other languages, I would write a get and set function instead.
Posted: Wed Mar 19, 2008 6:40 am
by jgraef
Hi,
Code: Select all
int x = 5;
if (fork()==0) {
while (x!=0);
printf("x is zero\n");
return 0;
}
sleep(5);
x = 0;
Posted: Wed Mar 19, 2008 7:04 am
by JamesM
jgraef wrote:Hi,
Code: Select all
int x = 5;
if (fork()==0) {
while (x!=0);
printf("x is zero\n");
return 0;
}
sleep(5);
x = 0;
Unfortunately fork() clones an address space, so the change in x would never be propagated between the processes. Nice try though. Possibly clone() would work better, but you still have the atomicity problem.
Posted: Wed Mar 19, 2008 7:33 am
by Solar
Combuster wrote:In all other languages, I would write a get and set function instead.
Or a wrapper. Unfortunately, in C there's only macros to do the trick.
Code: Select all
#define callFunc( x ) if ( x == 0 ) do_something();
callFunc( procedure1() );
callFunc( procedure2() );
Voila.
Posted: Wed Mar 19, 2008 7:40 am
by JamesM
oooh, ugly.