New C operator -- would it be a good idea?

M

Malcolm McLean

בת×ריך ×™×•× ×¨×‘×™×¢×™, 12 בספטמבר 2012 18:43:10 UTC+1, מ×ת Edward A. Falk:
(Although I wouldn't mind built-in abs(), min(), and max() functions, I admit
it's too late for even those.)
You could have |x| syntax. That would look a bit more mathematical.
 
I

Ike Naar

?????? ??? ?????, 12 ??????? 2012 18:43:10 UTC+1, ??? Edward A. Falk:
You could have |x| syntax. That would look a bit more mathematical.

In combination with the '|' and '||' operators it would lead
to ambiguous syntax, e.g.

|x||y||z|

could parse as

abs(x || y || z)

or as

abs(x) | y | abs(z)
 
K

Keith Thompson

Malcolm McLean said:
בת×ריך ×™×•× ×¨×‘×™×¢×™, 12 בספטמבר 2012 18:43:10 UTC+1, מ×ת Edward A. Falk:
You could have |x| syntax. That would look a bit more mathematical.

I doubt that it would be possible to add that syntax without
conflicting with the existing "|" and "||" operators. I'm nearly
certain that it would, at the very least, cause confusion.

There's no reason you couldn't add new operators whose symbols happen
to be keywords. "sizeof", at least when applied to an expression,
is a unary operator like "-", "~", or "!".

For example, a "min" operator might have the syntax `x min y`.
To apply it to multiple values, just write `x min y min z`, which
would be equivalent to `(x min y) min z`. "abs" could be a unary
operator rather than a built-in function; in that case, both `abs x`
and `abs(x)` would be valid.

Presumably the new keywords would have to be something like _Abs,
_Min, and _Max, with a new header defining appropriate macros.

I'm not necessarily saying these *should* be added to the language,
just speculating about a way it could be done.
 
B

Ben Bacarisse

Ike Naar said:
In combination with the '|' and '||' operators it would lead
to ambiguous syntax, e.g.

|x||y||z|

could parse as

abs(x || y || z)

or as

abs(x) | y | abs(z)

Not if they were treated like other C tokens. C has a rule that causes
its lexer to recognise the longest sequence of characters that make up a
valid token. Thus |x||y||z| must be seen as this sequence of tokens: |,
x, ||, y, ||, z, and |. Depending on the syntax, that would either be
an error (if |...| expressions can't not contain || operators) or it
would parse as your first example.
 
J

JohnF

Ben Bacarisse said:
Not if they were treated like other C tokens. C has a rule that causes
its lexer to recognise the longest sequence of characters that make up a
valid token.

This just wasn't a great idea in the first place.
Even mathematical notation usually doesn't split
a single operator into two pieces. Besides |x|
(and ||x|| if you think that's different), nothing
else is occurring to me, offhand. abs( ) is way better.
 
J

John Bode

In combination with the '|' and '||' operators it would lead
to ambiguous syntax, e.g.

|x||y||z|

could parse as

abs(x || y || z)

or as

abs(x) | y | abs(z)

Well, there's the question of where |x| would fit in the expression hierarchy;
should it be classed as primary expression, postfix expression, cast
expression, what? That would determine how it would be parsed.

C just wasn't designed with an eye towards mathematical notation, though.
I agree with Keith; use a keyword as the operator, and then it would
slip in very nicely with other unary operators:

unary-expression:
postfix-expression
'++' unary-expression
'--' unary-expression
unary-operator cast-expression
'sizeof' unary-expression
'sizeof' '(' type-name ')'
'_Alignof' '(' type-name ')'
'abs' unary-expression

It's just a question of how much demand there would be for such a feature.
 
B

Ben Pfaff

(Although I wouldn't mind built-in abs(), min(), and max() functions, I admit
it's too late for even those.)

Old versions of GNU C++ had <? and >? operators for min and max,
though they've been removed from current versions.
 
R

Rui Maciel

mike3 said:
Now, I was wondering: would this be a good
idea for a new real operator for the C language? Namely, "a -->
b" (and "b <-- a") as real operators that could increment a toward b.
Or would it be pretty worthless?

Your suggestion appears to be a solution looking for a problem.

If you wish to iterate through a statement based on how a couple of floating
point values compare between themselves then you already have at least two
concise ways of doing it: the while() statement and the for() statement.


Rui Maciel
 
K

Kaz Kylheku

בת×ריך ×™×•× ×¨×‘×™×¢×™, 12 בספטמבר 2012 18:43:10 UTC+1, מ×ת Edward A. Falk:
You could have |x| syntax. That would look a bit more mathematical.

Yes, nothing is more important than /looking/ mathematical, even while the
underlying semantics of the expression blows up whenever something hits
INT_MAX.

How about |x|, where x == INT_MIN, and INT_MIN is something like -32768 under
a 16 bit int. Oops!

Looking mathematical, very funny!

You know, none of these idiotic operators being proposed are going to make
one iota of difference to the overall productivity of C development.
These are just syntactic sugars that operate on a microscopic scale.

Language features which can make some kind of game changing difference
are those which reshape the organization of the program on a larger scale.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top