Jakub M.
Jakub M.

Reputation: 33827

Left hand side function, illegal pattern

I have a function that returns a function. I check it with EUnit test:

string_to_options_test() ->
    Parser = get_parser("-", ?OPTS_FOO_BAR),
    {ok, [opt_foo, opt_bar]} = Parser("foo-bar").

It is all right. When I modify the last line, I get an error:

string_to_options_test() ->                        % 41
    Parser = get_parser("-", ?OPTS_FOO_BAR),
    Parser("foo-bar") = {ok, [opt_foo, opt_bar]}.  % 43, swapped LHS and RHS,

Running the test:

urlparser.erl:43: illegal pattern
urlparser.erl:41: Warning: variable 'Parser' is unused

Why I can't use Parser function at the left hand side of the assignment?

Upvotes: 1

Views: 165

Answers (1)

troutwine
troutwine

Reputation: 3821

This is fun. The LHS expression of every pattern match must be a pattern while the right may be an arbitrary term. The primary difference is that a pattern may have unbound variables but must be fully reduced. Your LHS is an expression which can't be computed at compile time--as some arithmetic expressions can be--and is not a valid pattern as a result.

Upvotes: 1

Related Questions