|
|
| Author |
Message |
< Erlang ~ illegal buard expression |
| jz87 |
Posted: Thu Jan 10, 2008 1:05 pm |
|
|
|
Joined: 10 Jan 2008
Posts: 3
|
I wrote the following code:
Code: if is_process_alive(Pid) -> ...
and got an illegal guard expresson error. I thought BIF are ok in guard expressions |
|
|
| Back to top |
|
| hao |
Posted: Thu Jan 10, 2008 3:23 pm |
|
|
|
User
Joined: 20 Aug 2007
Posts: 18
Location: Uppsala, Sweden
|
It seems that BIF is_process_alive/1 is not allowed in guard tests for "if" control statement as well as for a function.
Code:
t(Pid) ->
Flag = is_process_alive(Pid),
if Flag == true ->
io:format("In t/1: the given process is alive.~n");
true ->
io:format("In t/1: the given process is NOT alive.~n")
end,
ok.
t/1 works well.
Code:
t1(Pid) when is_process_alive(Pid) ->
io:format("In t1/1: the given process is alive.~n"),
ok;
t1(_Pid) ->
io:format("In t1/1: the given process is NOT alive.~n"),
ok.
t1/1 compilation fails and gets illegal guard expression exception.
Code:
t2(Pid) when is_process_alive(Pid) == true ->
io:format("In t2/1: the given process is alive.~n"),
ok;
t2(_Pid) ->
io:format("In t2/1: the given process is NOT alive.~n"),
ok.
t2/1 compilation fails and gets illegal guard expression exception as well. |
|
|
| Back to top |
|
| bluefly |
Posted: Thu Jan 10, 2008 7:26 pm |
|
|
|
User
Joined: 06 Jan 2008
Posts: 10
|
I just checked the man for it ("erl -man erlang" or http://www.erlang.org/doc/man/erlang.html), and it looks like is_process_alive/1 is one of many BIFs that do not have the "Allowed in guard tests" line.
I remember reading in a tutorial somewhere that the BIFs are generally allowed... ah, I was wrong. I found this line:
"Only a few built in functions can be used in guards, and you cannot use functions you have defined yourself in guards."
http://erlang.org/doc/getting_started/seq_prog.html#2.12 |
|
|
| Back to top |
|
| hao |
Posted: Fri Jan 11, 2008 10:11 am |
|
|
|
User
Joined: 20 Aug 2007
Posts: 18
Location: Uppsala, Sweden
|
|
| 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
|
|
|