When (x==0) do_something();

All off topic discussions go here. Everything from the funny thing your cat did to your favorite tv shows. Non-programming computer questions are ok too.
User avatar
piranha
Member
Member
Posts: 1391
Joined: Thu Dec 21, 2006 7:42 pm
Location: Unknown. Momentum is pretty certain, however.
Contact:

When (x==0) do_something();

Post 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
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post 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? :lol:
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
User avatar
piranha
Member
Member
Posts: 1391
Joined: Thu Dec 21, 2006 7:42 pm
Location: Unknown. Momentum is pretty certain, however.
Contact:

Post 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
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post 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.
Every good solution is obvious once you've found it.
User avatar
piranha
Member
Member
Posts: 1391
Joined: Thu Dec 21, 2006 7:42 pm
Location: Unknown. Momentum is pretty certain, however.
Contact:

Post by piranha »

I know, it would make angry program and compiler.
It could be used for debugging though

-JL
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
User avatar
JoeKayzA
Member
Member
Posts: 79
Joined: Wed Aug 24, 2005 11:00 pm
Location: Graz/Austria

Post 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.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post 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...?
Every good solution is obvious once you've found it.
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

Post 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!");
}
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post 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!*
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post 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?
Every good solution is obvious once you've found it.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
jgraef
Member
Member
Posts: 47
Joined: Wed Jan 16, 2008 7:37 am
Location: Wonsheim, Germany

Post 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;
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post 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.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post 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. :wink:
Every good solution is obvious once you've found it.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

oooh, ugly.
Post Reply