Are there any languages in which the ternary operator can be used to modify code structure?
I am curious to know if there are any languages in which the ternary operator can be used to modify code structure at run time. Such as:
boolean bool = true;
// Addition method - can add 2 or 3 integers.
add(5,10 bool ? ) : ,15);
I would assume that if this exists anywhere, both the if 开发者_运维技巧and else statements of the ternary must be acceptable at compile-time.
No, because
such a language would be a syntactical nightmare
the intended effect can easily be produced by using
result = bool ? add( 5, 10 ) : add( 5, 10, 15 ) # both expressions evaluated?
result = add( 5, 10, bool ? 0 : 15 ) # maybe nil, empty, or nix instead of 0
the standard if of the language
精彩评论