Topic: C++0x Wish List ('when' keyword)


Author: David Schwartz <davids@webmaster.com>
Date: Thu, 1 Aug 2002 03:59:24 GMT
Raw View
Alisdair Meredith wrote:

> Of course, this proposal goes on stage further and raises many questions
> about it's use that I would not be happy answering.

> What is the scope of a when clause?
> What happens if I put two (or more) when's on the same variable?
> What happens if an exception is thrown in the when-clause?  Is the
> variable set or not?
> How do I unregister the when?
>
> There are dangers of hidden with when clauses on two variables.

 You can already achieve this affect easily:

class myInt
{
 private:
 int x;

 public:
 void SetX(int);
 int GetX(void) const;
 void operator=(int a) { SetX(a); }
 ...
};

 In the 'SetX' code, you can check a linked list of 'when's if you want
to. You can code any means for registering and unregistering 'when's.
You can do anything you need to if an exception in the 'when' is
encountered, including roll-back. You could have 'when's that roll-back
and 'when's that don't.

 Unfortunately, you fundamentally have to designate what things are
permitted to have 'when's or the language isn't C++. Consider, for
example, a 'when' applied to a member of a structure and then a 'memcpy'
that changes that structure.

 DS

---
[ 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                       ]





Author: Jonathan Geisler <geisler@nasser.ece.nwu.edu>
Date: Fri, 28 Jun 2002 18:58:37 GMT
Raw View
mackrune <rune420_removethis_@start.no> wrote:
: I've thought about this idea for some time, and while it might be hard to
: implement in a good way, it's a pretty original idea.
: The 'when' keyword would attach itself to a variable, and triggering an
: action when that variable gets a specific value. One idea is the following
: syntax:

Why can't you do this by overloading the = operator on the variable
that you're interested in watching.  In fact, I'm sure someone could
make a nice when template that takes an object, action, and value and
does this while the watched object remains in scope.

      -- Jonathan Geisler --

---
[ 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                       ]





Author: Hyman Rosen <hyrosen@mail.com>
Date: Fri, 28 Jun 2002 21:40:20 GMT
Raw View
Witless wrote:
> How soon does this suggestion produce a come_from statement?

Shh. Be vewy, vewy quiet. We're hunting wabbit! :-)

---
[ 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                       ]





Author: Christopher Eltschka <celtschk@web.de>
Date: Fri, 28 Jun 2002 21:43:20 GMT
Raw View
rmaddox@isicns.com (Randy Maddox) writes:

> "mackrune" <rune420_removethis_@start.no> wrote in message news:<af8bop$kld$1@troll.powertech.no>...
> > I've thought about this idea for some time, and while it might be hard to
> > implement in a good way, it's a pretty original idea.
> > The 'when' keyword would attach itself to a variable, and triggering an
> > action when that variable gets a specific value.
>
> > Well, any comments? I personally think this idea is pretty original,
> > although it might be hard to implement, but it would cut down codesize in
> > some cases, as I am sure you can imagine.
>
> While I agree that this certainly is an original idea, although it is
> available as a feature in many debuggers, it is not likely to ever
> become part of C++.

But it could be reduced to something the compiler itself can do, even
in a more general form:

If you use the syntax

  when (condition; statement)
  {
    statement1;
    statement2;
    statement3;
    statement4;
  }

then it could be defined to be equivalent to:

  {
    bool __triggered = false;

    statement1;
    if (!__triggered && condition) { statement; __triggered=true; }

    statement2;
    if (!__triggered && condition) { statement; __triggered=true; }

    statement3;
    if (!__triggered && condition) { statement; __triggered=true; }

    statement4;
    if (!__triggered && condition) { statement; __triggered=true; }
  }

This would have certain advantages over playing games with hardware
detection:

* It would be implementable on every machine where current C++
  is implementable (because it's semantics is the same as that of
  currently legal, although tedious to write, C++ code)

* It wouldn't cause problems with optimization (the point where the
  variable's memory address is changed may be different from the place
  where the variable is changed; we don't want to disallow
  optimization!

* Due to the well-defined places where the condition may be triggered
  (the end of each statement), there are no problems with when
  statements throwing exceptions (the throw will be synchronous)

* Functions called from within the construct are not affected; the
  condition is only checked after returning from them.


I've used ';' to separate the arguments of the assumed control
statement 'when', along the lines of the control statement 'for'
(which currently is the only control statement taking more than one
argument).

[...]

---
[ 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                       ]





Author: rmaddox@isicns.com (Randy Maddox)
Date: Tue, 25 Jun 2002 21:35:35 GMT
Raw View
"mackrune" <rune420_removethis_@start.no> wrote in message news:<af8bop$kld$1@troll.powertech.no>...
> I've thought about this idea for some time, and while it might be hard to
> implement in a good way, it's a pretty original idea.
> The 'when' keyword would attach itself to a variable, and triggering an
> action when that variable gets a specific value.

> Well, any comments? I personally think this idea is pretty original,
> although it might be hard to implement, but it would cut down codesize in
> some cases, as I am sure you can imagine.

While I agree that this certainly is an original idea, although it is
available as a feature in many debuggers, it is not likely to ever
become part of C++.

In order to catch the change of the variable value to the desired
value it would be necessary to trap all writes to the location of the
variable in memory.  This can be done, obviously since many debuggers
do support this, but it cannot be done in any standardized way.  The
common approach is to allocate the variable in a page of memory that
is marked as read-only.  An attempt to write to that location then
results in a fault of some type, and that fault can be directed to a
handler that can reset the page to writable, set the value, and reset
the page back to read-only.  Clearly this fault handler could also
look for the specified value and somehow indicate when that value was
written, maybe by setting a flag someplace.  Of course, you would
probably also need an independent thread reading that flag to take the
requested action.

Once you get down to the level of threads and memory page attributes
you are below the level of a high-level language like C++.

Good luck with this one.

>
>
> ---
> [ 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                       ]

---
[ 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                       ]





Author: "Rob Staveley \(Tom\)" <rstaveley@seseit.com>
Date: Tue, 25 Jun 2002 21:36:51 GMT
Raw View
If 'variable' was a protected member of a class, which could only be
assigned using a public function, you'd have something pretty easy to hook
with a test.

"mackrune" <rune420_removethis_@start.no> wrote in message
news:af8bop$kld$1@troll.powertech.no...
> I've thought about this idea for some time, and while it might be hard to
> implement in a good way, it's a pretty original idea.
> The 'when' keyword would attach itself to a variable, and triggering an
> action when that variable gets a specific value. One idea is the following
> syntax:
> when( variable, value, action );    // when 'variable' turns into 'value'
> perform 'action'
>
> And have the 'when' last out the current scope. Alternately (perhaps
> better), 'when' could have its own scope, like this:
> when( variable, value, action ) { }    // same as above, with own scope
>
> To make when more useful, instead of value, there could be a function to
> test if the variable has gotten it's value, like this:
> when( variable, function, action ) { }    // function is user defined of
> type 'bool foo(variable_type &variable)'
>
> // example:
> class A
> {
>     // ...
> }
>
> bool TestA(A &test)
> {
>     return ( /* test A for something */ ) ? true : false;
> }
>
> void DoSomething()
> {
>     // ...
> }
>
> void foo
> {
>     A bar;
>     when(bar, TestA, DoSomething)
>     {
>         // ...
>     }
> }
> // You get the picture...
>
> 'action' would not necesarilly have to be function, it could also be a
> statement, like 'return false' or 'break'.
>
> Well, any comments? I personally think this idea is pretty original,
> although it might be hard to implement, but it would cut down codesize in
> some cases, as I am sure you can imagine.
>
>
> ---
> [ 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                       ]
>


---
[ 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                       ]





Author: Alisdair Meredith <alisdair.sp-am.bl-ock.meredith@uk.renaultf1.com>
Date: Wed, 26 Jun 2002 16:33:20 GMT
Raw View
This is vaguly similar to the idea of a property as implemented in many
RAD environments/languages, where setting a property can have a side
effect.

Of course, this proposal goes on stage further and raises many questions
about it's use that I would not be happy answering.

What is the scope of a when clause?
What happens if I put two (or more) when's on the same variable?
What happens if an exception is thrown in the when-clause?  Is the
variable set or not?
How do I unregister the when?

There are dangers of hidden with when clauses on two variables.

I also dislike it on 'gut instinct' as it separates cause and effect.
The variable may be declared in one file, its value may be set in
another, and a third developer may set up a when clause in yet another
file.  I would not like to diagnose bugs in this environment, as it is
never clear that the code you are reading is all the code that is
relevant.  There is no connection between the code changing the
variable, and any other code that may have chosen to register a 'when'.

The idea that changing a value has consequences that you want to chain
has merit, but I feel would be best handled by making that value-type
into a class that supports such behaviour.

AlisdairM

mackrune wrote:
>
> I've thought about this idea for some time, and while it might be hard to
> implement in a good way, it's a pretty original idea.
> The 'when' keyword would attach itself to a variable, and triggering an
> action when that variable gets a specific value. One idea is the following
> syntax:
> when( variable, value, action );    // when 'variable' turns into 'value'
> perform 'action'
>
> And have the 'when' last out the current scope. Alternately (perhaps
> better), 'when' could have its own scope, like this:
> when( variable, value, action ) { }    // same as above, with own scope
>
> To make when more useful, instead of value, there could be a function to
> test if the variable has gotten it's value, like this:
> when( variable, function, action ) { }    // function is user defined of
> type 'bool foo(variable_type &variable)'
>
> // example:
> class A
> {
>     // ...
> }
>
> bool TestA(A &test)
> {
>     return ( /* test A for something */ ) ? true : false;
> }
>
> void DoSomething()
> {
>     // ...
> }
>
> void foo
> {
>     A bar;
>     when(bar, TestA, DoSomething)
>     {
>         // ...
>     }
> }
> // You get the picture...
>
> 'action' would not necesarilly have to be function, it could also be a
> statement, like 'return false' or 'break'.
>
> Well, any comments? I personally think this idea is pretty original,
> although it might be hard to implement, but it would cut down codesize in
> some cases, as I am sure you can imagine.

---
[ 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                       ]





Author: "mackrune" <rune420_removethis_@start.no>
Date: Wed, 26 Jun 2002 22:47:07 GMT
Raw View
Alisdair Meredith wrote:
> What is the scope of a when clause?

As I wrote, it would be smart to give 'when' it's own scope, or perhaos have
it last out the current one.
when(variable, value, action) { /* this is whens scope */ }

> What happens if I put two (or more) when's on the same variable?

It's easy to design rules for this.

void f();
void x();
int i = 0;
when(i, 123, f)
{
    // ...
    when(i, 123, x)
    {
        //....
    }
    // ...
}

Here there are two options:
1. Ignore the first 'when' in the 2nd 'when'.
2. Execute f() first, then x(), if i gets value 123 in the 2nd 'when'.

> What happens if an exception is thrown in the when-clause?  Is the
> variable set or not?

Then an exception is thrown in a 'when' clause! What happens if an exception
is thrown in a for-loop?

> How do I unregister the when?

You end it's scope.

> There are dangers of hidden with when clauses on two variables.
>
> I also dislike it on 'gut instinct' as it separates cause and effect.
> The variable may be declared in one file, its value may be set in
> another, and a third developer may set up a when clause in yet another
> file.  I would not like to diagnose bugs in this environment, as it is
> never clear that the code you are reading is all the code that is
> relevant.  There is no connection between the code changing the
> variable, and any other code that may have chosen to register a
> 'when'.

This would not be much of a problem, since 'when' never actually changes the
value of the variable, just checks it whenever its value changes.

> The idea that changing a value has consequences that you want to chain
> has merit, but I feel would be best handled by making that value-type
> into a class that supports such behaviour.

As I have already said, 'when' never actually changes the value (if the
value isn't a global, and the function changes it, ofcourse).

mackrune


---
[ 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                       ]





Author: Hyman Rosen <hyrosen@mail.com>
Date: Wed, 26 Jun 2002 23:47:56 GMT
Raw View
mackrune wrote:
> The 'when' keyword would attach itself to a variable, and triggering an
> action when that variable gets a specific value.

It would also be good to have another version which attaches
itself to a label, and triggers when execution passes through
that label. The INTERCAL programming language has a similar
statement.

---
[ 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                       ]





Author: Witless <witless@attbi.com>
Date: Thu, 27 Jun 2002 15:50:39 GMT
Raw View
Hyman Rosen wrote:

> mackrune wrote:
> > The 'when' keyword would attach itself to a variable, and triggering an
> > action when that variable gets a specific value.
>
> It would also be good to have another version which attaches
> itself to a label, and triggers when execution passes through
> that label. The INTERCAL programming language has a similar
> statement.

How soon does this suggestion produce a come_from statement?

---
[ 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                       ]





Author: "mackrune" <rune420_removethis_@start.no>
Date: Tue, 25 Jun 2002 06:53:35 GMT
Raw View
I've thought about this idea for some time, and while it might be hard to
implement in a good way, it's a pretty original idea.
The 'when' keyword would attach itself to a variable, and triggering an
action when that variable gets a specific value. One idea is the following
syntax:
when( variable, value, action );    // when 'variable' turns into 'value'
perform 'action'

And have the 'when' last out the current scope. Alternately (perhaps
better), 'when' could have its own scope, like this:
when( variable, value, action ) { }    // same as above, with own scope

To make when more useful, instead of value, there could be a function to
test if the variable has gotten it's value, like this:
when( variable, function, action ) { }    // function is user defined of
type 'bool foo(variable_type &variable)'

// example:
class A
{
    // ...
}

bool TestA(A &test)
{
    return ( /* test A for something */ ) ? true : false;
}

void DoSomething()
{
    // ...
}

void foo
{
    A bar;
    when(bar, TestA, DoSomething)
    {
        // ...
    }
}
// You get the picture...

'action' would not necesarilly have to be function, it could also be a
statement, like 'return false' or 'break'.

Well, any comments? I personally think this idea is pretty original,
although it might be hard to implement, but it would cut down codesize in
some cases, as I am sure you can imagine.


---
[ 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                       ]