Guards and side effects

Luke Gorrie luke@REDACTED
Fri Mar 11 12:44:42 CET 2005


"Ulf Wiger \(AL/EAB\)" <ulf.wiger@REDACTED> writes:

> I think it is perfectly reasonable and sound to clearly
> differentiate between functions that modify data (and perhaps
> produce side-effects) and expressions that test the validity
> of data in a side-effect free manner.

I'm with you, I like guards the way they are.

I think the main issue is that guards look so nice:

  foo(X) when guard1(X) -> code1;
  foo(X) when guard2(X) -> code2;
  ...

but if you need a non-guard test then it's way more verbose:

    foo(X) ->
        case test1(X) of
            true ->
                code1;
            false ->
                case test2(X) of
                    true ->
                        code2;
                    false ->
                        ...
                end
        end

Seems a bit unnecessary.

The related and bigger problem for me is code like:

    case foo() of
        {ok, F} ->
            case bar() of
                {ok, B} ->
                    ... ;
                Err = {error, Reason} ->
                    Err
            end;
        Err = {error, Reason} ->
            Err
    end

I'm looking forward to trying out the new try-catch on this code once
we manage to upgrade to R10.

Cheers,
Luke





More information about the erlang-questions mailing list