[erlang-questions] The If expression

Robert Virding <>
Thu Apr 22 03:39:12 CEST 2010


On 22 April 2010 03:08, Henning Diedrich <> wrote:
>>
>> Pure functional languages like Haskell and Clean don't have one-armed
>> ifs because every expression has to have a value, and both arms of an
>> if have to have values of the same type.
>
> It's funny you say that because it seems like /all/ spots where I ran into
> this issue are one of
>
>   * output
>   * message sending
>   * global names registration
>
> ... David's example adds
>
>   * creating a connection
>
> ... also an external state issue.
>
> All samples seem to be about /creating a side effect/ ---  or, under certain
> conditions, not creating it.
>
> Is that a legitimate opening for a one-branched if? Exactly for the less
> functionally-clean stuff, on Erlang's more practical leanings?

The problem is that however you choose to use 'if', or any other
construction for that matter, Erlang is a functional language and
everything returns, and must return, a value. So even if 'if' didn't
need to have an explicit default or else case then it would still have
to return a value. We would never be able agree on a default value
anyway. :-) 'nil' is not a good value here as it has no inherent
meaning in Erlang as it does in Lisp.

It would also mean that 'if' would not behave consistently with 'case'
and function clauses which generate an error if there is no matching
clause. This I think would be a serious mistake.

Robert


More information about the erlang-questions mailing list