I ported most of my application to OTP behavior, but I'm stuck. I cannot figure out how to do selective transfers using gen_server. If none of the callback function suggestions matches the message, but puts the message in the mailbox, it does not work!
Now, wherever I go, people welcome a selective welcome. Everywhere I go, people welcome OTP. Is it really true that you cannot have both at once? Doesn't that sound like a serious, fixable flaw?
How do erlang programmers handle this?
EDIT (response to zed comment):
Here is an example where I would like to see a list of integers printed in sorted order:
-module(sel_recv). -behaviour(gen_server). -export([start_link/0]). -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). -export([test/0]). start_link() -> gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). test() -> gen_server:cast(?MODULE, test). init([]) -> {ok, 0}. handle_call(_Request, _From, State) -> Reply = ok, {reply, Reply, State}. handle_cast(test, _State) -> lists:map(fun(N) -> gen_server:cast(?MODULE, {result, N}) end, [9,8,7,6,5,4,3,2,1]), {noreply, [1,2,4,5,6,7,8,9]}; handle_cast({result, N}, [N|R]) -> io:format("result: " ++ integer_to_list(N) ++ "~n"), {noreply, R}. handle_info(_Info, State) -> {noreply, State}. terminate(_Reason, _State) -> ok. code_change(_OldVsn, State, _Extra) -> {ok, State}.
Of course, in my real application, there are timer delays, and messages that need to be processed in order alternate with other messages. In particular, I send HTTP requests, sometimes a lot at once, sometimes one at a time with an interval between them. In any case, I need to collect them in order.