[comp.lang.c++] simplifying C++

kearns@softrue.UUCP (Steven Kearns) (02/20/91)

Here is another in the continuing series:  "Simplifying C++".  

Expression languages are languages where each statement returns a value.
Lets make C++ an expression language as follows:

	Expression Statements, like "a = 5;", return the value of the 
		expression.
	"if (e1) s1; else s2;" returns the value of s1 if e1 true, otherwise
		it returns the value of s2.  If s1 and s2 
		return values of different types, the "if" returns a void.
	"{s1; s2; s3;} returns the value of the last statement in the
		compound statement. 
	All other statements, including an if statement without an else,
		return void.

The advantage of this is manyfold: first, we get to eliminate the
comma operator, which is used to evaluate multiple expressions as in
(e1, e2, e3).  Instead, you can write {e1; e2; e3;}.  Also, we can
eliminate the ugly (B ? e1 : e2) syntax, writing (if (B) e1; else e2;) 
instead.

Also, you can do things impossible before, for example declaring
variables inside expressions:

	if {int i = f(a, b); i >=3 && i <= 33;}
		cout << "hi there";

We probably want to disallow "gotos" and "returns" inside a compound
statement used as an expression.  

-steve

********************************************************
* Steven Kearns            ....uunet!softrue!kearns    *
* Software Truth           softrue!kearns@uunet.uu.net *
********************************************************

tor@bcars171.UUCP (Rene Tio) (02/20/91)

In article <5.UUL1.3#8618@softrue.UUCP>, kearns@softrue.UUCP (Steven Kearns) writes:
>
>Here is another in the continuing series:  "Simplifying C++".  
>
>Expression languages are languages where each statement returns a value.
[explanation deleted...]
>The advantage of this is manyfold: first, we get to eliminate the
>comma operator, which is used to evaluate multiple expressions as in
>(e1, e2, e3).  Instead, you can write {e1; e2; e3;}.  Also, we can
>eliminate the ugly (B ? e1 : e2) syntax, writing (if (B) e1; else e2;) 
>instead.
>
>Also, you can do things impossible before, for example declaring
>variables inside expressions:
>
>	if {int i = f(a, b); i >=3 && i <= 33;}
>		cout << "hi there";
>
>We probably want to disallow "gotos" and "returns" inside a compound
>statement used as an expression.  
>
>-steve
...
Being an advocate of functional programming styles, I am all for such a
change.  After several years of data flow languages I really miss such
constructs.  However, I can see several problems with this:

a. You can't disallow gotos and returns inside a compound statement unless
   you disallow it from all compound statements.  Otherwise, you end up with
   an inconsistent syntax for anything in between braces.

b. Expression languages invariably bring up the question of arity. How would
   you return more than one value from a function? e.g.,
        a, b = f(x);
	....
	int, int f(int y)
	{
	    return y+1,y+2;
	}
   Lisp gets around this by returning lists (sort of a dynamic structure). 
   Other programs have a syntax similar to that above.

What is really needed is a concerted effort to evolve C along more functional
lines.  That would alleviate many of the problems with porting C to parallel
applications.

weikart@prl.dec.com (Chris Weikart) (03/06/91)

In article <5.UUL1.3#8618@softrue.UUCP>, kearns@softrue.UUCP (Steven
Kearns) writes:
|> 
|> Here is another in the continuing series:  "Simplifying C++".  
|> 
|> Expression languages are languages where each statement returns a
|> value.
|> Lets make C++ an expression language as follows:
|> 
|> 	Expression Statements, like "a = 5;", return the value of the 
|> 		expression.
|> 	"if (e1) s1; else s2;" returns the value of s1 if e1 true,
|> otherwise
|> 		it returns the value of s2.  If s1 and s2 
|> 		return values of different types, the "if" returns a void.
|> 	"{s1; s2; s3;} returns the value of the last statement in the
|> 		compound statement. 
|> 	All other statements, including an if statement without an else,
|> 		return void.
|> 
|> The advantage of this is manyfold: first, we get to eliminate the
|> comma operator, which is used to evaluate multiple expressions as in
|> (e1, e2, e3).  Instead, you can write {e1; e2; e3;}.  Also, we can
|> eliminate the ugly (B ? e1 : e2) syntax, writing (if (B) e1; else
|> e2;) 
|> instead.
|> 
|> Also, you can do things impossible before, for example declaring
|> variables inside expressions:
|> 
|> 	if {int i = f(a, b); i >=3 && i <= 33;}
|> 		cout << "hi there";
|> 
|> We probably want to disallow "gotos" and "returns" inside a compound
|> statement used as an expression.  
|> 
|> -steve
|> 
|> ********************************************************
|> * Steven Kearns            ....uunet!softrue!kearns    *
|> * Software Truth           softrue!kearns@uunet.uu.net *
|> ********************************************************

The comma operator must have been invented for "for" loops; it's
usefulness in macros was, in my opinion, unforeseen and fortuitous.
"For" loops require a separator with lower (or higher? I can never get
this straight) priority than the ";" in order to allow multiple
expressions for each of the three parts.

-------------------------------------------------------------------------------
Chris Weikart <weikart@dec.prl.com> .............. DEC Paris Research Lab (PRL)