|
|
| Author |
Message |
|
| anders_n |
Posted: Tue Jul 04, 2006 4:09 pm |
|
|
|
User
Joined: 28 Feb 2005
Posts: 155
Location: Saltillo, Mexico
|
Hi
I recently gave an Erlang course and one of my students found
the following.
I would have expected a compiler error in this case. But as You
can see it compiles and runs with a somewhat unexpected result.
A similar error is a case statement gives a compiler error.
-module(if_bug).
-export([foo/1]).
foo(X) ->
if
X > 1 ->
ok; % <- Note semicolon here
not_ok;
true ->
true
end.
Erlang (BEAM) emulator version 5.5 [source] [async-threads:0] [hipe]
12> c(if_bug).
{ok,if_bug}
13> if_bug:foo(2).
ok
14>
/Anders
Post generated from www.trapexit.org |
|
|
| Back to top |
|
| Guest |
Posted: Tue Jul 04, 2006 5:57 pm |
|
|
|
Guest
|
Hi,
> foo(X) ->
> if
> X > 1 ->
> ok; % <- Note semicolon here
> not_ok;
> true ->
> true
> end.
This is because "not_ok; true" is a valid guard expression and guards
have higher priority in the parser than the if clause....
Can be confusing, I agree.
best regards,
Vlad
Post generated from www.trapexit.org |
|
|
| Back to top |
|
| Guest |
Posted: Tue Jul 04, 2006 9:52 pm |
|
|
|
Guest
|
Anders Nygren wrote:
>
> foo(X) ->
if
X > 1 ->
ok; % <- Note semicolon here
not_ok;
true ->
true
end.
You are being misled by the indentation.
If you write it like this, it's easier to see:
foo(X) ->
if
X > 1 ->
ok; % <- Note semicolon here
not_ok; true ->
true
end.
Semicolon within a guard separates disjunctions, i.e.,
it's an "or" operator. It can't be nested, and has
lower precedence than comma ("and").
It would have made a nice submission to the Obfuscated
Erlang Contest, though.
/Richard
Post generated from www.trapexit.org |
|
|
| Back to top |
|
|
|
All times are GMT
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|
|