gen_server -- newbie code critique?

Vance Shipley vances@REDACTED
Mon May 29 20:23:48 CEST 2006


Andrew,

Your use of State has some style problems.

On Sun, May 28, 2006 at 04:04:11AM -0700, Andrew Lentvorski wrote:
}
}  -record(state, {theQueue}).
}  
}  init([]) ->
}     {ok, #state{theQueue=queue:new()}}.
}  
}  handle_call({in, InObj}, _From, State) ->
}     QR = queue:in(InObj, State#state.theQueue),
}     UpdatedState = #state{theQueue=QR},
}     {reply, ok, UpdatedState};

You are using a record to store the State however it contains only
one field.  Until you need more fields you might as well get rid
of the record:

init([]) ->
	{ok, queue:new()}.


However if you do plan on adding fields later keep the #state{}
record but then you'll need to update it instead of recreating it:

handle_call({in, InObj}, _From, State) ->
	QR = queue:in(InObj, State#state.theQueue),
	{reply, ok, State#state{theQueue = QR}};


	-Vance



More information about the erlang-questions mailing list