Conditional Assignment Operator?

Started by
6 comments, last by mossmoss 18 years, 11 months ago
Do any programming languages have a conditional assignment operator? I am not aware of any, but that doesn't mean anything. I'm designing my own little programming language, sort of a cross between C and Basic along with anything else I feel like throwing in. I don't expect it to be especially useful to anyone, just for personal experimentation with parsing techniques and other things, and possibly to try some genetic programming. But to the point: What are your thoughts on a conditional asignment operator? How would the syntax look? Maybe something like this: foo <?= bar; // if (foo < bar) foo = bar; Is it even a useful idea and worth the effort to implement? Thanks, ProgrammingHobbit
ProgrammingHobbit------------------Don't hate me because i'm a Hobbit.If you already know C++, then you already know the D Programming Language. Its Awesome!!
Advertisement
foo = (foo < bar ? bar : foo);
There's no languages that I know of. The closest I can think of is C/C++'s:
foo < bar ? foo=bar : false;

I think a conditional assignment operator would just make the source code hard to read to be honest.
Nice idea, but I would recommend not to put too many operators in your language. It makes the language harder to learn and the programs harder to read.
Especially if you can easily write expressions in another way, i.e.: foo = max(foo, bar)
Quote:Original post by Evil Steve
I think a conditional assignment operator would just make the source code hard to read to be honest.


It would just be something new to learn wouldn't it? Once you get used to it there would be no difficulty would there?

I know its not hard to do conditional assignment in a normal way. Is it worth more operators in a language to make it easier?

Thanks for the replies.
ProgrammingHobbit
ProgrammingHobbit------------------Don't hate me because i'm a Hobbit.If you already know C++, then you already know the D Programming Language. Its Awesome!!
Quote:Original post by ProgrammingHobbit
Is it worth more operators in a language to make it easier?


Is it used very often in your programs ?
Quote:Original post by nmi
Is it used very often in your programs ?


Not especially. I don't really know if a conditional assignment operator would even be useful. Thats why I'm asking.

Thanks,
ProgrammingHobbit
ProgrammingHobbit------------------Don't hate me because i'm a Hobbit.If you already know C++, then you already know the D Programming Language. Its Awesome!!
Quote:Original post by ProgrammingHobbit
I know its not hard to do conditional assignment in a normal way. Is it worth more operators in a language to make it easier?


Depends; it's a risk-vs-reward thing. In this particular case, I'd say no, it's not worth it, since it's pretty trivial as is, especially if you wrap its intent into a properly named function/macro. pirate_dau and nmi had the right answers.

This topic is closed to new replies.

Advertisement