Re: an algorithm with interruption in the middle
Andrei Alexandrescu (See Website For Email) wrote:
Seungbeom Kim wrote:
Declarations and scopes are static concepts, so mixing them with runtime
behaviour seems too confusing.
I think this is an exaggeration. There is no mixing going on. We have
runtime stuff and we have lexical stuff. The two are governed by
different rules.
As long as you ban declarations in the second operand of &&, ||
and in the second and third operands of ?:.
Besides, I use the feature in Perl all the time and there's not once I
got confused.
Oh, I didn't know that you could write
use strict;
my $a = 2 ? (my $b = 3) : (my $c = 4);
in Perl. It just introduces $b and $c into scope, without actually
initializing $c (because the condition is true).
And I feel that most of cases where I needed declarations buried in
expressions have been in conditional expressions with if, while, etc.
Otherwise, you can just use a simple-declaration, without introducing
any change of the scope.
Can you give an example of the "many other instances"?
cin >> float f;
scanf("%f", &(float f));
Trim(cin >> string line);
factory.CreateInstance(&(Object * p = 0));
Object * p = new Object[size_t newLength = length * 2];
So, how are they different from below?
float f; cin >> f;
float f; scanf("%f", &f));
string line; Trim(cin >> line);
Object* p = 0; factory.CreateInstance(&p);
size_t newLength = length * 2; Object *p = new Object[newLength];
A bit more typing, but there are no lexical or semantic differences.
Lexical in that the scope extends until the end of the enclosing block,
which is different from the cases for conditional expressions:
some people want to use an extra pair of braces to limit the scope, e.g.
// statements...
{
bool done = false;
while (!done) {
// set done to true somewhere
}
}
// statements...
--
Seungbeom Kim
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]