[erlang-questions] Inline If

Masklinn masklinn@REDACTED
Sun May 9 11:05:00 CEST 2010


On 2010-05-09, at 01:20 , Zoltan Lajos Kis wrote:
> I guess the ternary operator only makes sense in non-functional languages, where conditional statements do not have return values.
> Given that there was no ternary operator, you would have to duplicate the assignment part, rendering the code less readable:
> 
> if (a) { x = b; } else { x = c; };
> /* x = a ? b : c; */
> 
> In a functional language, such as Erlang, the these statements do have a return value, so the ternary-operator would only be a syntactic sugar:
> 
> X = case A of true -> B; false -> C end.

For that precise case, using if would probably be a bit shorter (thought the second case might not be as readable):

X = if A -> B; true -> C end.



More information about the erlang-questions mailing list