Erlang badarith input / output

I am learning Erlang and I am trying to pass a message from one process to another with some input from stdio input.

This is the code I have (I know that I can use the usual functions, but this is not the topic).

-module(play).

-compile(export_all).

calculateArea() ->
  receive
    {rectangle, W, H,SenderPID} -> SenderPID ! {area,W * H};
    {circle, R,SenderPID} -> SenderPID ! {area,3.14 * R * R};
    _ -> io:format("We can only calculate area of rectangles or circles.")
  end,
  calculateArea().

doStuff() ->
  CalculateArea = spawn(play,calculateArea,[]),
  {ok,Width} = io:fread("Enter width","~d"),
  {ok,Height} = io:fread("Enter height","~d"),
  CalculateArea ! {rectangle,Width,Height,self()},
  receive
    {area,Size} -> io:write(Size)
  end,
  io:fwrite("done").

When I start play:doStuff()., I get an error {badarith,[{play,calculateArea,0,[{file,"play.erl"},{line,10}]}]}.

I don’t understand why, according to the docs, “~ d” will give me a decimal value, and it definitely looks like if I print it out.

What's going on here?

+4
source share
1 answer

io:fread returns

Result = {ok, Terms :: [term()]}
       | {error, {fread, FreadError :: io_lib:fread_error()}}
       | server_no_data()

So Widthand Heightare lists containing one of each number. Correct using {ok, [Width/Height]} = ....

+4
source

Source: https://habr.com/ru/post/1611478/


All Articles