[erlang-questions] extending function clauses

Koushik Narayanan koushik.list@REDACTED
Fri Dec 19 16:16:42 CET 2008


Hi,

On Fri, Dec 19, 2008 at 05:59:58AM -0800, Ittay Dror wrote:
> 
> Hi,
> 
> Is it possible to add clauses to a function that is defined in another
> module?

AFAIK No.

> 
> More elaborately: say a 3rd party module 'geometry' defines a function
> 'area', for each shape (shapes are defined as tuples where the first element
> is an atom naming the shape) there is a clause (so there is a clause for
> 'circle', 'square' etc.). Now, I have a new shape. Can I extend
> 'geometry:area' so it can handle my new shape (by adding a new clause). I
> cannot change the original module's file. 'geometry:area' is used by other
> functions to do more sophisticated stuff (so defining my own area function
> is not effective). If not, what is the erlang way of handling this
> situation? 

For such a problem you could define a new module say geometry_new whos shape function handles the new clause alone,
and for other clauses calles geometry:shape like

shape({new_shape,..}) ->
	do_the_new_stuff;
	
shape(Anything_else) ->
	geometry:shape(Anthing_else).

HTH,

Koushik Narayanan



More information about the erlang-questions mailing list