ID:132345
 
How hard would it be to make it so that the if() proc can return the value from its condition? For examples:

x = if(5+5)

x = if(my_testcondition_proc())

It seems like this could clean up code, but I don't know how hard it would be to implement.

Cheers,

-- John M.
var/x=0
if(pass())x=1
What exactly do you want the if() statement to do? Is what you're requesting any different from just directly setting the variable to the conditional value? Or do you still want to run code under the if() statement?

The more typical approach in other languages is to have the assignment operator ('=') also return the assigned value. For example:

var/ID
if((ID = getID()))
world<<"The ID is [ID]"
else
world<<"You do not have an ID"


However, I'm sure that would be a significant change to the engine, and is more useful in a pointer-based language than it would be in BYOND.

If you really wanted to code that way (it would look strange with a code block under the if(), imho), you could probably whip up a macro:

#define RET_IF(_value_) _value_;if(_value_)

mob/verb/Test()
var/ID = RET_IF(src.key)
src<<"You have the key [ID]! You aren't a bot!"


Keep in mind that this macro will break some things, especially if-if-else chains. Also, anything you pass it will have to be calculated/called twice.

This macro avoid the second problem, but you can't combine it with the assignment operator:

#define SET_IF(_var_,_value_) _var_ = _value_;if(_var_)

mob/verb/Test2()
var/ID
SET_IF(ID, src.key)
src<<"You have the key [ID]! You aren't a bot!"
In response to DarkCampainger
DarkCampainger wrote:
What exactly do you want the if() statement to do? Is what you're requesting any different from just directly setting the variable to the conditional value? Or do you still want to run code under the if() statement?


Would still run code under the if() statement. It just seems like this could be a way to eliminate 1 line of redundant code in certain cases. Like I said, I have no idea how hard it would be to implement for BYOND.
var/x = !!(5 + 5)
What scope does that variable live in? Is it's life-time only inside the if block, if/elseif/else blocks, or the current scope before you enter the if block? It's all rather confusing.

x = if (5+5) and x = 5+5; if (x)
would both be the same to the compiler (or very similar) in opcodes, similar enough performance-wise to make no real odds certainly. Similarly the "productivity" factor is very mininal, having worked with languages much more verbose and strict than BYOND I've found it's the big language constructs that waste / save you time, not little things like this.

I'm not sure this carries enough benefit to be worth both the implementation time and the programmer confusion, I'm afraid.