Newbie help please with variable unsafe in 'receive' & variable unused

classic Classic list List threaded Threaded
21 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Newbie help please with variable unsafe in 'receive' & variable unused

Donald Steven
Hi everyone,

Can someone please put me out of my (extended) misery?  The part of the
code that's causing the problem is:

=============================

loop(Orbiter1) ->

     receive

         {Orbiter1, Coordinates} -> X = element(1, element(1,
Coordinates)), Y = element(2, element(1, Coordinates)),
                                    Z = element(3, element(1, Coordinates))
     end,

         io:format("Coordinates: ~p~n", [Coordinates]),
         io:format("X: ~p, Y: ~p, Z: ~p~n", [X, Y, Z])

     loop(Orbiter1).

=============================

which produces the error:

base.erl:48: variable 'Coordinates' unsafe in 'receive' (line 35)
base.erl:37: Warning: variable 'X' is unused
base.erl:37: Warning: variable 'Y' is unused
base.erl:38: Warning: variable 'Z' is unused

{"init terminating in
do_boot",{undef,[{base,main,[],[]},{init,start_em,1,[]},{init,do_boot,3,[]}]}}
init terminating in do_boot ()

=============================

Thanks for your help.

Don
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Newbie help please with variable unsafe in 'receive' & variable unused

Vlad Dumitrescu-2
Hi!

What you probably want to do is

loop(Orbiter1) ->
    Coordinates = receive
        {Orbiter1, Coordinates} -> Coordinates
    end,
    {X, Y, Z} = Coordinates,
    io:format("Coordinates: ~p~n", [Coordinates]),
    io:format("X: ~p, Y: ~p, Z: ~p~n", [X, Y, Z])
    loop(Orbiter1).

regards,
Vlad


On Thu, Sep 29, 2016 at 4:22 PM, Donald Steven <[hidden email]> wrote:
Hi everyone,

Can someone please put me out of my (extended) misery?  The part of the code that's causing the problem is:

=============================

loop(Orbiter1) ->

    receive

        {Orbiter1, Coordinates} -> X = element(1, element(1, Coordinates)), Y = element(2, element(1, Coordinates)),
                                   Z = element(3, element(1, Coordinates))
    end,

        io:format("Coordinates: ~p~n", [Coordinates]),
        io:format("X: ~p, Y: ~p, Z: ~p~n", [X, Y, Z])

    loop(Orbiter1).

=============================

which produces the error:

base.erl:48: variable 'Coordinates' unsafe in 'receive' (line 35)
base.erl:37: Warning: variable 'X' is unused
base.erl:37: Warning: variable 'Y' is unused
base.erl:38: Warning: variable 'Z' is unused

{"init terminating in do_boot",{undef,[{base,main,[],[]},{init,start_em,1,[]},{init,do_boot,3,[]}]}}
init terminating in do_boot ()

=============================

Thanks for your help.

Don
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Newbie help please with variable unsafe in 'receive' & variable unused

Donald Steven

Alas that produces:

problem.erl:26: Warning: variable 'Coordinates' exported from 'receive' (line 26)
{"init terminating in do_boot",{{badmatch,{{-4.998000e+02,6.000000e-01,2.000000e-01}}},[{problem,loop,1,[{file,"problem.erl"},{line,29}]},{init,start_em,1,[]},{init,do_boot,3,[]}]}}
init terminating in do_boot ()


On 09/29/2016 10:34 AM, Vlad Dumitrescu wrote:
loop(Orbiter1) ->
    Coordinates = receive
        {Orbiter1, Coordinates} -> Coordinates
    end,
    {X, Y, Z} = Coordinates,
    io:format("Coordinates: ~p~n", [Coordinates]),
    io:format("X: ~p, Y: ~p, Z: ~p~n", [X, Y, Z])
    loop(Orbiter1).


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Newbie help please with variable unsafe in 'receive' & variable unused

Vlad Dumitrescu-2
The crash is because the message doesn't look like I assumed. I didn't pay attention and didn't see that it should be {{X, Y, Z}} = Coordinates (two levels of braces)

To get rid of the warning, you can rewrite 

loop(Orbiter1) ->
    receive
        {Orbiter1, Coordinates={{X,Y,Z}}} -> 
            io:format("Coordinates: ~p~n", [Coordinates]),
            io:format("X: ~p, Y: ~p, Z: ~p~n", [X, Y, Z])
    end,
    loop(Orbiter1).

For a real server, you probably want to handle more messages, and ignore those you don't care about. For example

loop(Orbiter1) ->
    receive
        {Orbiter1, Coordinates={{X,Y,Z}}} -> 
            io:format("Coordinates: ~p~n", [Coordinates]),
            io:format("X: ~p, Y: ~p, Z: ~p~n", [X, Y, Z]),
            loop(Orbiter1);
        _ ->
            ok % stop if unknown message arrives
    end.

regards,
Vlad


On Thu, Sep 29, 2016 at 4:52 PM, Donald Steven <[hidden email]> wrote:

Alas that produces:

problem.erl:26: Warning: variable 'Coordinates' exported from 'receive' (line 26)
{"init terminating in do_boot",{{badmatch,{{-4.998000e+02,6.000000e-01,2.000000e-01}}},[{problem,loop,1,[{file,"problem.erl"},{line,29}]},{init,start_em,1,[]},{init,do_boot,3,[]}]}}
init terminating in do_boot ()


On 09/29/2016 10:34 AM, Vlad Dumitrescu wrote:
loop(Orbiter1) ->
    Coordinates = receive
        {Orbiter1, Coordinates} -> Coordinates
    end,
    {X, Y, Z} = Coordinates,
    io:format("Coordinates: ~p~n", [Coordinates]),
    io:format("X: ~p, Y: ~p, Z: ~p~n", [X, Y, Z])
    loop(Orbiter1).



_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Newbie help please with variable unsafe in 'receive' & variable unused

Donald Steven

Thanks Vlad, that works.  However, there are really 3 orbiters and I need to use the values of X1, Y1, Z1, X2, Y2, Z2, and X3, Y3 and Z3 outside of the 'receive', to compare them, calculate distances between points, etc.

When I try:

loop(Orbiter1, Orbiter2, Orbiter3) ->

    receive
        {Orbiter1, Coordinates = {{X1, Y1, Z1}}} ->                 % the io:format statements are just fro debugging.  What I really need
            io:format("Coordinates: ~p~n", [Coordinates]),            % are the X1, Y1, and Z1 coordinates so that I can do some work with them
            io:format("X1: ~p, Y1: ~p, Z1: ~p~n", [X1, Y1, Z1]);    % beyond the end of the 'receive'
        {Orbiter2, Coordinates = {{X2, Y2, Z2}}} ->
            io:format("Coordinates: ~p~n", [Coordinates]),
            io:format("X2: ~p, Y2: ~p, Z2: ~p~n", [X2, Y2, Z2]);
        {Orbiter3, Coordinates = {{X3, Y3, Z3}}} ->
            io:format("Coordinates: ~p~n", [Coordinates]),
            io:format("X3: ~p, Y3: ~p, Z3: ~p~n", [X3, Y3, Z3]);
        _ ->
            ok % stop if unknown message arrives
    end,

    io:format("X1: ~p, Y1: ~p, Z1: ~p~n", [X1, Y1, Z1]),        % this would really be more substantial code.  This is a placeholder
    loop(Orbiter1, Orbiter2, Orbiter3).

=================

I get:

problem.erl:48: variable 'X1' unsafe in 'receive' (line 34)
problem.erl:48: variable 'Y1' unsafe in 'receive' (line 34)
problem.erl:48: variable 'Z1' unsafe in 'receive' (line 34)
{"init terminating in do_boot",{undef,[{problem,main,[],[]},{init,start_em,1,[]},{init,do_boot,3,[]}]}}
init terminating in do_boot ()

=================

but if I move the 'loop(Orbiter1, Orbiter2, Orbiter3)' statement back within the clauses in 'receive', it never reaches the code beyond 'end,'.

&^(**(^(*(&\&%$^&%$%^$!!!!

In everyday English, I want to:

1.  Get the X, Y and Z coordinates from each Orbiter (the concurrent processes that are producing them (verified correctly))
2.  Do some work with the coordinates (e.g., calculating how far apart the orbiters are)

Don


On 09/29/2016 11:02 AM, Vlad Dumitrescu wrote:
loop(Orbiter1) ->
    receive
        {Orbiter1, Coordinates={{X,Y,Z}}} -> 
            io:format("Coordinates: ~p~n", [Coordinates]),
            io:format("X: ~p, Y: ~p, Z: ~p~n", [X, Y, Z]),
            loop(Orbiter1);
        _ ->
            ok % stop if unknown message arrives
    end.


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Newbie help please with variable unsafe in 'receive' & variable unused [SOLVED]

Donald Steven
In reply to this post by Vlad Dumitrescu-2

This works (and is simple!):

loop(Orbiter1, Orbiter2, Orbiter3) ->

        receive {Orbiter1,{{X1, Y1, Z1}}} -> ok end,
        receive {Orbiter2,{{X2, Y2, Z2}}} -> ok end,
        receive {Orbiter3,{{X3, Y3, Z3}}} -> ok end,

    io:format("X1: ~p, Y1: ~p, Z1: ~p~n", [X1, Y1, Z1]),
    io:format("X2: ~p, Y2: ~p, Z2: ~p~n", [X2, Y2, Z2]),
    io:format("X3: ~p, Y3: ~p, Z3: ~p~n", [X3, Y3, Z3]),

    loop(Orbiter1, Orbiter2, Orbiter3).


On 09/29/2016 11:02 AM, Vlad Dumitrescu wrote:
The crash is because the message doesn't look like I assumed. I didn't pay attention and didn't see that it should be {{X, Y, Z}} = Coordinates (two levels of braces)

To get rid of the warning, you can rewrite 

loop(Orbiter1) ->
    receive
        {Orbiter1, Coordinates={{X,Y,Z}}} -> 
            io:format("Coordinates: ~p~n", [Coordinates]),
            io:format("X: ~p, Y: ~p, Z: ~p~n", [X, Y, Z])
    end,
    loop(Orbiter1).

For a real server, you probably want to handle more messages, and ignore those you don't care about. For example

loop(Orbiter1) ->
    receive
        {Orbiter1, Coordinates={{X,Y,Z}}} -> 
            io:format("Coordinates: ~p~n", [Coordinates]),
            io:format("X: ~p, Y: ~p, Z: ~p~n", [X, Y, Z]),
            loop(Orbiter1);
        _ ->
            ok % stop if unknown message arrives
    end.

regards,
Vlad


On Thu, Sep 29, 2016 at 4:52 PM, Donald Steven <[hidden email]> wrote:

Alas that produces:

problem.erl:26: Warning: variable 'Coordinates' exported from 'receive' (line 26)
{"init terminating in do_boot",{{badmatch,{{-4.998000e+02,6.000000e-01,2.000000e-01}}},[{problem,loop,1,[{file,"problem.erl"},{line,29}]},{init,start_em,1,[]},{init,do_boot,3,[]}]}}
init terminating in do_boot ()


On 09/29/2016 10:34 AM, Vlad Dumitrescu wrote:
loop(Orbiter1) ->
    Coordinates = receive
        {Orbiter1, Coordinates} -> Coordinates
    end,
    {X, Y, Z} = Coordinates,
    io:format("Coordinates: ~p~n", [Coordinates]),
    io:format("X: ~p, Y: ~p, Z: ~p~n", [X, Y, Z])
    loop(Orbiter1).




_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Newbie help please with variable unsafe in 'receive' & variable unused

Vlad Dumitrescu-2
In reply to this post by Donald Steven
Hi Donald,

I see. If you need to do processing that involves all three sets of coordinates, you can't do it like that because after each receive you can only know one set, the one just received. You have to keep the coords in  the server state. This is a simple alternative (sorry for the short names). 

% State is [{Orbiter1, {X1, Y1, Z1}}, ...]
orbiter(State) ->
    receive 
        {O, C} ->
            State1 = updateState(State, {O, C}),
            process_state(State1),
            orbiter(State1);
        _ ->
           % something
           ok
    end.

This is quite standard simple server, I suggest you check a good introduction to that. http://learnyousomeerlang.com/ is my favorite.

regards,
Vlad


On Thu, Sep 29, 2016 at 6:29 PM, Donald Steven <[hidden email]> wrote:

Thanks Vlad, that works.  However, there are really 3 orbiters and I need to use the values of X1, Y1, Z1, X2, Y2, Z2, and X3, Y3 and Z3 outside of the 'receive', to compare them, calculate distances between points, etc.

When I try:

loop(Orbiter1, Orbiter2, Orbiter3) ->

    receive
        {Orbiter1, Coordinates = {{X1, Y1, Z1}}} ->                 % the io:format statements are just fro debugging.  What I really need
            io:format("Coordinates: ~p~n", [Coordinates]),            % are the X1, Y1, and Z1 coordinates so that I can do some work with them
            io:format("X1: ~p, Y1: ~p, Z1: ~p~n", [X1, Y1, Z1]);    % beyond the end of the 'receive'
        {Orbiter2, Coordinates = {{X2, Y2, Z2}}} ->
            io:format("Coordinates: ~p~n", [Coordinates]),
            io:format("X2: ~p, Y2: ~p, Z2: ~p~n", [X2, Y2, Z2]);
        {Orbiter3, Coordinates = {{X3, Y3, Z3}}} ->
            io:format("Coordinates: ~p~n", [Coordinates]),
            io:format("X3: ~p, Y3: ~p, Z3: ~p~n", [X3, Y3, Z3]);
        _ ->
            ok % stop if unknown message arrives
    end,

    io:format("X1: ~p, Y1: ~p, Z1: ~p~n", [X1, Y1, Z1]),        % this would really be more substantial code.  This is a placeholder
    loop(Orbiter1, Orbiter2, Orbiter3).

=================

I get:

problem.erl:48: variable 'X1' unsafe in 'receive' (line 34)
problem.erl:48: variable 'Y1' unsafe in 'receive' (line 34)
problem.erl:48: variable 'Z1' unsafe in 'receive' (line 34)
{"init terminating in do_boot",{undef,[{problem,main,[],[]},{init,start_em,1,[]},{init,do_boot,3,[]}]}}
init terminating in do_boot ()

=================

but if I move the 'loop(Orbiter1, Orbiter2, Orbiter3)' statement back within the clauses in 'receive', it never reaches the code beyond 'end,'.

&^(**(^(*(&\&%$^&%$%^$!!!!

In everyday English, I want to:

1.  Get the X, Y and Z coordinates from each Orbiter (the concurrent processes that are producing them (verified correctly))
2.  Do some work with the coordinates (e.g., calculating how far apart the orbiters are)

Don


On 09/29/2016 11:02 AM, Vlad Dumitrescu wrote:
loop(Orbiter1) ->
    receive
        {Orbiter1, Coordinates={{X,Y,Z}}} -> 
            io:format("Coordinates: ~p~n", [Coordinates]),
            io:format("X: ~p, Y: ~p, Z: ~p~n", [X, Y, Z]),
            loop(Orbiter1);
        _ ->
            ok % stop if unknown message arrives
    end.



_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Newbie help please with variable unsafe in 'receive' & variable unused

Donald Steven
Thanks Vlad, I'll give it a whirl!

On 9/29/2016 2:53 PM, Vlad Dumitrescu wrote:
Hi Donald,

I see. If you need to do processing that involves all three sets of coordinates, you can't do it like that because after each receive you can only know one set, the one just received. You have to keep the coords in  the server state. This is a simple alternative (sorry for the short names). 

% State is [{Orbiter1, {X1, Y1, Z1}}, ...]
orbiter(State) ->
    receive 
        {O, C} ->
            State1 = updateState(State, {O, C}),
            process_state(State1),
            orbiter(State1);
        _ ->
           % something
           ok
    end.

This is quite standard simple server, I suggest you check a good introduction to that. http://learnyousomeerlang.com/ is my favorite.

regards,
Vlad


On Thu, Sep 29, 2016 at 6:29 PM, Donald Steven <[hidden email]> wrote:

Thanks Vlad, that works.  However, there are really 3 orbiters and I need to use the values of X1, Y1, Z1, X2, Y2, Z2, and X3, Y3 and Z3 outside of the 'receive', to compare them, calculate distances between points, etc.

When I try:

loop(Orbiter1, Orbiter2, Orbiter3) ->

    receive
        {Orbiter1, Coordinates = {{X1, Y1, Z1}}} ->                 % the io:format statements are just fro debugging.  What I really need
            io:format("Coordinates: ~p~n", [Coordinates]),            % are the X1, Y1, and Z1 coordinates so that I can do some work with them
            io:format("X1: ~p, Y1: ~p, Z1: ~p~n", [X1, Y1, Z1]);    % beyond the end of the 'receive'
        {Orbiter2, Coordinates = {{X2, Y2, Z2}}} ->
            io:format("Coordinates: ~p~n", [Coordinates]),
            io:format("X2: ~p, Y2: ~p, Z2: ~p~n", [X2, Y2, Z2]);
        {Orbiter3, Coordinates = {{X3, Y3, Z3}}} ->
            io:format("Coordinates: ~p~n", [Coordinates]),
            io:format("X3: ~p, Y3: ~p, Z3: ~p~n", [X3, Y3, Z3]);
        _ ->
            ok % stop if unknown message arrives
    end,

    io:format("X1: ~p, Y1: ~p, Z1: ~p~n", [X1, Y1, Z1]),        % this would really be more substantial code.  This is a placeholder
    loop(Orbiter1, Orbiter2, Orbiter3).

=================

I get:

problem.erl:48: variable 'X1' unsafe in 'receive' (line 34)
problem.erl:48: variable 'Y1' unsafe in 'receive' (line 34)
problem.erl:48: variable 'Z1' unsafe in 'receive' (line 34)
{"init terminating in do_boot",{undef,[{problem,main,[],[]},{init,start_em,1,[]},{init,do_boot,3,[]}]}}
init terminating in do_boot ()

=================

but if I move the 'loop(Orbiter1, Orbiter2, Orbiter3)' statement back within the clauses in 'receive', it never reaches the code beyond 'end,'.

&^(**(^(*(&\&%$^&%$%^$!!!!

In everyday English, I want to:

1.  Get the X, Y and Z coordinates from each Orbiter (the concurrent processes that are producing them (verified correctly))
2.  Do some work with the coordinates (e.g., calculating how far apart the orbiters are)

Don


On 09/29/2016 11:02 AM, Vlad Dumitrescu wrote:
loop(Orbiter1) ->
    receive
        {Orbiter1, Coordinates={{X,Y,Z}}} -> 
            io:format("Coordinates: ~p~n", [Coordinates]),
            io:format("X: ~p, Y: ~p, Z: ~p~n", [X, Y, Z]),
            loop(Orbiter1);
        _ ->
            ok % stop if unknown message arrives
    end.






Avast logo

This email has been checked for viruses by Avast antivirus software.
www.avast.com



_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Newbie help please with variable unsafe in 'receive' & variable unused

Richard A. O'Keefe-2
In reply to this post by Donald Steven
On 30/09/16 3:22 AM, Donald Steven wrote:
[The basic problem is that
    % Tag is defined here, Datum and Value are not
    receive {Tag,Datum} -> Value = f(Datum) end,
    use(Datum, Value)
  produces warnings about unsafe variables.]

If a variable is defined in some branch(es) of a case or receive
but not others, it is in scope after the case or receive, but as
it might not be defined, it is unsafe.

In this case, it looks to me as if the compiler is wrong.
There is no path through the receive that doesn't define all
the variables.

You could write

     loop(Orbiter1) ->
         Coordinates = receive {Orbiter1, C} -> C end,
         V = element(1, Coordinates),
         X = element(1, V),
         Y = element(2, V),
         Z = element(3, V),
         io:format("Coordinates: ~p~n", [Coordinates]),
         io:format("X: ~p, Y: ~p, Z: ~p~n", [X, Y, Z]),
         loop(Orbiter1).

which the compiler is happy with.
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Newbie help please with variable unsafe in 'receive' & variable unused

Richard A. O'Keefe-2
In reply to this post by Donald Steven


On 30/09/16 5:29 AM, Donald Steven wrote:

>
> loop(Orbiter1, Orbiter2, Orbiter3) ->
>
>     receive
>         {Orbiter1, Coordinates = {{X1, Y1, Z1}}} ->                 %
> the io:format statements are just fro debugging.  What I really need
>             io:format("Coordinates: ~p~n", [Coordinates]),            %
> are the X1, Y1, and Z1 coordinates so that I can do some work with them
>             io:format("X1: ~p, Y1: ~p, Z1: ~p~n", [X1, Y1, Z1]);    %
> beyond the end of the 'receive'
>         {Orbiter2, Coordinates = {{X2, Y2, Z2}}} ->
>             io:format("Coordinates: ~p~n", [Coordinates]),
>             io:format("X2: ~p, Y2: ~p, Z2: ~p~n", [X2, Y2, Z2]);
>         {Orbiter3, Coordinates = {{X3, Y3, Z3}}} ->
>             io:format("Coordinates: ~p~n", [Coordinates]),
>             io:format("X3: ~p, Y3: ~p, Z3: ~p~n", [X3, Y3, Z3]);
>         _ ->
>             ok % stop if unknown message arrives
>     end,
>
>     io:format("X1: ~p, Y1: ~p, Z1: ~p~n", [X1, Y1, Z1]),        % this
> would really be more substantial code.  This is a placeholder
>     loop(Orbiter1, Orbiter2, Orbiter3).

At your "placeholder" line, if an {Orbiter2, {{X2,Y2,Z2}}} message
arrived, what values do you expect X1,Y1,Z1 to have, and why do
you expect them to have those values?

I would suggest code like

loop(Orbiter1, Orbiter2, Orbiter3) ->
     receive
         {Orbiter1, {XYZ}} -> handle(1, Orbiter1, XYZ)
       ; {Orbiter2, {XYZ}} -> handle(2, Orbiter2, XYZ)
       ; {Orbiter3, {XYZ}} -> handle(3, Orbiter3, XYZ)
       ; _Other                  -> ignored
     end,
     loop(Orbiter1, Orbiter2, Orbiter3).

handle(Orbiter_Index, Orbiter, XYZ) ->
   % debugging
     io:format("Orbiter ~d coordinates = ~p~n",
         [Orbiter_Index, XYZ},
   % real work
     ok.

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Newbie question: function include/1 undefined

Donald Steven
In reply to this post by Donald Steven
Hi all,

I can get '-include(' to work before the main() function, but I can't
get it to work *in* a function, either using a comma or a period at end.

--------------------------

This works (before main()):

-include("filename.hrl").

but neither of these work:

main() ->

-include("filename.hrl").
-include("filename.hrl"),

--------------------------

Thanks for your help.

Don

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Newbie question: function include/1 undefined

Stanislaw Klekot
On Fri, Sep 30, 2016 at 09:25:49AM -0400, Donald Steven wrote:
> Hi all,
>
> I can get '-include(' to work before the main() function, but I
> can't get it to work *in* a function, either using a comma or a
> period at end.

It doesn't work that way. This is a compiler's directive, not
a statement. It's interpreted at the compilation time, not when you
execute code.

What do you actually want to achieve with this?

--
Stanislaw Klekot
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Newbie question: function include/1 undefined

Richard A. O'Keefe-2
In reply to this post by Donald Steven


On 1/10/16 2:25 AM, Donald Steven wrote:
> Hi all,
>
> I can get '-include(' to work before the main() function, but I can't
> get it to work *in* a function, either using a comma or a period at end.

The syntax of Erlang does not allow this.
An Erlang source file is a sequence of chunks each terminated by
a full stop.
A chunk is a function definition if it does not begin with "-";
if it does begin with "-" it's a declaration like -module or
-export or a preprocessor directive like -include.

These things *cannot* be mixed.

Can you tell us what you are trying to achieve by doing this?

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Newbie question: function include/1 undefined

Donald Steven
Thanks Richard.  I had (alas) come to that conclusion.

In c (or m4 or the like), I can include blocks of text or code quite
freely.  In this case, primarily as a matter of aesthetics, I wanted to
off load some repetitive initializations to a file which could be
included at the appropriate point, with a neat % comment on the side to
keep my head straight.  It's a big, ugly block in an otherwise elegant
set of funs.  I guess I'm stuck with it, as I'm reluctant to make things
too complicated by running the whole thing through m4 first before the
erlang pre-processor.  I do wish it could be done though.  (Dare I
suggest a modest proposal?)

On 10/2/2016 7:26 PM, Richard A. O'Keefe wrote:

>
>
> On 1/10/16 2:25 AM, Donald Steven wrote:
>> Hi all,
>>
>> I can get '-include(' to work before the main() function, but I can't
>> get it to work *in* a function, either using a comma or a period at end.
>
> The syntax of Erlang does not allow this.
> An Erlang source file is a sequence of chunks each terminated by
> a full stop.
> A chunk is a function definition if it does not begin with "-";
> if it does begin with "-" it's a declaration like -module or
> -export or a preprocessor directive like -include.
>
> These things *cannot* be mixed.
>
> Can you tell us what you are trying to achieve by doing this?
>
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions


---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Newbie question: function include/1 undefined

Richard A. O'Keefe-2


On 3/10/16 12:37 PM, Donald Steven wrote:
> Thanks Richard.  I had (alas) come to that conclusion.
>
> In c (or m4 or the like), I can include blocks of text or code quite
> freely.

I used to maintain pdm4.

> In this case, primarily as a matter of aesthetics, I wanted to
> off load some repetitive initializations to a file which could be
> included at the appropriate point, with a neat % comment on the side to
> keep my head straight.

Colour me stupid, but I don't see why you can't have

    % biginit.hrl

    biginit(Arguments...) ->
       big,
       ugly,
       block.


    % main.erl
    ...
    -include('biginit.erl').

    main(...) ->
       biginit(...),
       rest of main.

OK, so it's *two* lines instead of one line, but is that a problem?

Failing that, what stops biginit being a single big ugly macro?
Again, at the point of use there would be two lines, not one.

I'm trying to think of anything I might want to include in the
body of a function that couldn't be in another function, and failing.


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Newbie question: function include/1 undefined

Donald Steven
Thanks.  I'll have another look.

On 10/2/2016 8:03 PM, Richard A. O'Keefe wrote:

>
>
> On 3/10/16 12:37 PM, Donald Steven wrote:
>> Thanks Richard.  I had (alas) come to that conclusion.
>>
>> In c (or m4 or the like), I can include blocks of text or code quite
>> freely.
>
> I used to maintain pdm4.
>
>> In this case, primarily as a matter of aesthetics, I wanted to
>> off load some repetitive initializations to a file which could be
>> included at the appropriate point, with a neat % comment on the side to
>> keep my head straight.
>
> Colour me stupid, but I don't see why you can't have
>
>    % biginit.hrl
>
>    biginit(Arguments...) ->
>       big,
>       ugly,
>       block.
>
>
>    % main.erl
>    ...
>    -include('biginit.erl').
>
>    main(...) ->
>       biginit(...),
>       rest of main.
>
> OK, so it's *two* lines instead of one line, but is that a problem?
>
> Failing that, what stops biginit being a single big ugly macro?
> Again, at the point of use there would be two lines, not one.
>
> I'm trying to think of anything I might want to include in the
> body of a function that couldn't be in another function, and failing.
>
>


---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Newbie question: function include/1 undefined

Donald Steven
In reply to this post by Richard A. O'Keefe-2
Richard,

The program is a musical composition.  There are three modules: base,
orbiter and composer.  The 16 orbiter processes fly elliptical orbits
and, when they're close, they 'resonate' and output musical material.  
The orbits have to be initialized, so you end up with this kind of sprawl:

main(Args) ->

     io:format("~nbase.erl, version 1.13, 2 October 2016~n,... working
... "),

     Orbiter1 = spawn(orbiter, start, []),
     Orbiter1 ! {self(), #orbit{xaxis =    780.0, yaxis = 187.0, xorigin
= 0.0, yorigin = 0.2, eccentricity = 0.7, rotation = 0.0, direction =
?ROTX, translation = 0.2, speed = 265}},

     Orbiter2 = spawn(orbiter, start, []),
     Orbiter2 ! {self(), #orbit{xaxis =    171.0, yaxis = 112.0, xorigin
= 0.1, yorigin = 0.3, eccentricity = 0.6, rotation = 0.1, direction =
?ROTY, translation = 0.3, speed = 111}},

     Orbiter3 = spawn(orbiter, start, []),
     Orbiter3 ! {self(), #orbit{xaxis =    40.0, yaxis = 140.0, xorigin
= 0.0, yorigin = 0.5, eccentricity = 0.5, rotation = 0.2, direction =
?ROTZ, translation = 0.4, speed = 63}},

... and so on (up to 16), before getting to more attractive code such as:

====================================================================================================================================

baseloop(         _,           _, _           , _,           _, ?DONE)
-> ok;
baseloop(SystemTime, OrbiterList, ComposerList, L, ?MAXORBITER, _    ) ->

     proximitytest(SystemTime, OrbiterList, ComposerList,
lists:reverse(L), 1, ?NOTDONE),
     baseloop(SystemTime, OrbiterList, ComposerList, [], 1, ?NOTDONE);

baseloop(SystemTime, OrbiterList, ComposerList, L,           I, ?NOTDONE) ->

     Orbiter = lists:nth(I, OrbiterList),
     receive
         {Orbiter, {{X, Y, Z}}} -> ok
     end,
     L1 = [{X, Y, Z} | L],

     CurrentTime = os:system_time(?HUNDREDTHS) - SystemTime,
     if CurrentTime >= ?LENGTHOFPIECE -> stopcomposers(ComposerList,
?NUMTRACKS),
                                         baseloop(SystemTime,
OrbiterList, ComposerList, L1, I + 1, ?DONE);
                                 true -> baseloop(SystemTime,
OrbiterList, ComposerList, L1, I + 1, ?NOTDONE)
     end.

====================================================================================================================================

My thinking was that, if I move this all outside main(), I'll have to go
through contortions to retrieve the pids (I trust that self() is
available everywhere in the module and doesn't have to be passed
explicitly from one fun to another).

I apologize if all this seems naive.  I'm was 'brought up' on c and am
transitioning to erlang, and just an old man having some fun.

Don




On 10/02/2016 08:03 PM, Richard A. O'Keefe wrote:

>
>
> On 3/10/16 12:37 PM, Donald Steven wrote:
>> Thanks Richard.  I had (alas) come to that conclusion.
>>
>> In c (or m4 or the like), I can include blocks of text or code quite
>> freely.
>
> I used to maintain pdm4.
>
>> In this case, primarily as a matter of aesthetics, I wanted to
>> off load some repetitive initializations to a file which could be
>> included at the appropriate point, with a neat % comment on the side to
>> keep my head straight.
>
> Colour me stupid, but I don't see why you can't have
>
>    % biginit.hrl
>
>    biginit(Arguments...) ->
>       big,
>       ugly,
>       block.
>
>
>    % main.erl
>    ...
>    -include('biginit.erl').
>
>    main(...) ->
>       biginit(...),
>       rest of main.
>
> OK, so it's *two* lines instead of one line, but is that a problem?
>
> Failing that, what stops biginit being a single big ugly macro?
> Again, at the point of use there would be two lines, not one.
>
> I'm trying to think of anything I might want to include in the
> body of a function that couldn't be in another function, and failing.
>
>

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Newbie question: function include/1 undefined

Richard A. O'Keefe-2
(1) Your initialisation code can return a tuple or list of process IDs.

(2) Your initialisation code can store the process IDs in the
     process dictionary.  Each process has its own dictionary.

     Self = self(),
     Orbiter1 = spawn(fun () -> orbiter:start(Self, data(1) end),
     ...
     Orbiter16 = spawn(fun () -> orbiter:start(Self, data(16) end),
     put(orbiter1, Orbiter1),
     ...
     put(orbiter16, Orbiter16),
     ...
     or even

     Self = self(),
     put(orbiters, [spawn(fun () -> orbiter:start(Self, Data) end)
                   || Data <- orbiter_data()])

(3) You could use the (per-node) global process registry in the
     initialisation code

     register(orbiter1, spawn(fun () -> ... end)),
     ...

     and then use orbiter1!Message later.

This seriously looks like code that could be in another module
without any preprocessor use at all.

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Newbie question: function include/1 undefined

Donald Steven
Thanks Richard and Jeff, much appreciated.


On 10/02/2016 11:14 PM, Richard A. O'Keefe wrote:

> (1) Your initialisation code can return a tuple or list of process IDs.
>
> (2) Your initialisation code can store the process IDs in the
>     process dictionary.  Each process has its own dictionary.
>
>     Self = self(),
>     Orbiter1 = spawn(fun () -> orbiter:start(Self, data(1) end),
>     ...
>     Orbiter16 = spawn(fun () -> orbiter:start(Self, data(16) end),
>     put(orbiter1, Orbiter1),
>     ...
>     put(orbiter16, Orbiter16),
>     ...
>     or even
>
>     Self = self(),
>     put(orbiters, [spawn(fun () -> orbiter:start(Self, Data) end)
>                   || Data <- orbiter_data()])
>
> (3) You could use the (per-node) global process registry in the
>     initialisation code
>
>     register(orbiter1, spawn(fun () -> ... end)),
>     ...
>
>     and then use orbiter1!Message later.
>
> This seriously looks like code that could be in another module
> without any preprocessor use at all.
>

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Newbie question: function include/1 undefined

Fred Hebert-2
In reply to this post by Donald Steven
On 10/02, Donald Steven wrote:

>Thanks Richard.  I had (alas) come to that conclusion.
>
>In c (or m4 or the like), I can include blocks of text or code quite
>freely.  In this case, primarily as a matter of aesthetics, I wanted
>to off load some repetitive initializations to a file which could be
>included at the appropriate point, with a neat % comment on the side
>to keep my head straight.  It's a big, ugly block in an otherwise
>elegant set of funs.  I guess I'm stuck with it, as I'm reluctant to
>make things too complicated by running the whole thing through m4
>first before the erlang pre-processor.  I do wish it could be done
>though.  (Dare I suggest a modest proposal?)
>

Another approach, although a bit more painful to make work and correlate
with all scope would be to use macros:

-define(CODE_SNIPPET, begin <whatever> end).

YOu can then -include("myheader.hrl"). and just call ?CODE_SNIPPET
inline.

Richard O'Keefe's solutions would be cleaner in the long run (and I
definitely recommend running with that option), but this could be an
immediate workaround for what you had in mind.
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
12