Erlang Mailing Lists

Author Message

<  Erlang  ~  illegal buard expression

jz87
Posted: Thu Jan 10, 2008 1:05 pm Reply with quote
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
View user's profile Send private message
hao
Posted: Thu Jan 10, 2008 3:23 pm Reply with quote
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.
View user's profile Send private message Send e-mail MSN Messenger
bluefly
Posted: Thu Jan 10, 2008 7:26 pm Reply with quote
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
View user's profile Send private message
hao
Posted: Fri Jan 11, 2008 10:11 am Reply with quote
User Joined: 20 Aug 2007 Posts: 18 Location: Uppsala, Sweden
What bluefly mentioned is right. We can find out all about the guard tests in the 6.24 Guard Sequences in the Erlang Reference Manual. (http://erlang.org/doc/reference_manual/expressions.html) Wink
View user's profile Send private message Send e-mail MSN Messenger

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

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