1
votes

I find myself doing this occasionally:

if some_condition then 
    expr 
else
    ()

Is there a nicer way to do this? It seems there should be a one-liner. (Running it all into one line doesn't count. By "one-liner" I mean "something which in one line indicates more clearly my intentions.")

1
I don't understand the question... If the type of expr is unit, then the else is entirely optional to begin with - ildjarn
Ah, man. I didn't know that! You should make that an answer, I suppose. - Søren Debois

1 Answers

3
votes

As demonstrated (rather poorly) in the documentation, if the final expression in the if has type unit then the else is optional. Thus your code can simply become:

if some_condition then expr