Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > New C operator -- would it be a good idea?

Reply
Thread Tools

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

 
 
Malcolm McLean
Guest
Posts: n/a
 
      09-14-2012
בתאריך יום רביעי, 12 בספטמבר 2012 18:43:10 UTC+1, מאת Edward A. Falk:
> In article <(E-Mail Removed)>,
>
>
> (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.
 
Reply With Quote
 
 
 
 
Ike Naar
Guest
Posts: n/a
 
      09-14-2012
On 2012-09-14, Malcolm McLean <(E-Mail Removed)> wrote:
> ?????? ??? ?????, 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.


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)
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      09-14-2012
Malcolm McLean <(E-Mail Removed)> writes:
> בתאריך יום רביעי, 12 בספטמבר 2012 18:43:10 UTC+1, מאת Edward A. Falk:
>> In article <(E-Mail Removed)>,
>> (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 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.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      09-14-2012
Ike Naar <(E-Mail Removed)> writes:

> On 2012-09-14, Malcolm McLean <(E-Mail Removed)> wrote:
>> ?????? ??? ?????, 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.

>
> 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.

--
Ben.
 
Reply With Quote
 
JohnF
Guest
Posts: n/a
 
      09-14-2012
Ben Bacarisse <(E-Mail Removed)> wrote:
> Ike Naar <(E-Mail Removed)> writes:
>> Malcolm McLean <(E-Mail Removed)> wrote:
>>>>
>>> You could have |x| syntax. That would look a bit more mathematical.

>>
>> In combination with the '|' and '||' operators it would lead
>> to ambiguous syntax

>
> 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.
--
John Forkosh ( mailto: (E-Mail Removed) where j=john and f=forkosh )
 
Reply With Quote
 
John Bode
Guest
Posts: n/a
 
      09-14-2012
On Friday, September 14, 2012 2:53:15 AM UTC-5, Ike Naar wrote:
> On 2012-09-14, Malcolm McLean <(E-Mail Removed)> wrote:
> > ?????? ??? ?????, 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.

>
> 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.
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      09-14-2012
(E-Mail Removed) (Edward A. Falk) writes:

> (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.
 
Reply With Quote
 
Rui Maciel
Guest
Posts: n/a
 
      09-14-2012
mike3 wrote:

> 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
 
Reply With Quote
 
Kaz Kylheku
Guest
Posts: n/a
 
      09-14-2012
On 2012-09-14, Malcolm McLean <(E-Mail Removed)> wrote:
> בתאריך יום רביעי, 12 בספטמבר 2012 18:43:10 UTC+1, מאת Edward A. Falk:
>> In article <(E-Mail Removed)>,
>>
>>
>> (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.


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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Would Anyone Use a _FuncRetType Operator? Shao Miller C Programming 10 02-25-2012 12:52 PM
What are the key differences between operator new and operator new[]? xmllmx C++ 6 02-03-2010 04:11 PM
an oddball scary kind of thing you would think would never happen richard Computer Support 4 01-31-2010 06:34 PM
Allocation with new operator and Destroying with free operator saikishore.vanga@wipro.com C++ 8 12-22-2005 01:35 PM
Would a ':=' operator break anything? Phil Tomson Ruby 6 04-09-2004 05:02 PM



Advertisments