the OO metaphor

Karlsson Mikael <>
Wed Nov 29 10:46:48 CET 2000


"Vlad Dumitrescu" <> writes:
.
.
> An example might be appropriate.
> 
> -module(base).
> 
> fun1(blip) -> foo;
> fun1(A) -> {empty, A}.
> 
> ==== and (the syntax is just a proposal)
> 
> -module(base_extended).
> -extend({base, [fun1]}).
> 
> fun1({help}) -> hohoho;
> -inherited(). %% need not be always present
> fun1(_) -> false.
.
.
> regards,
> Vlad
> 

Hi Vlad,

Without extending the erlang syntax, how about something
like?:

-module(extend_test).
-export([test1/1,test2/1]).

%% Basic framework stuff
extend_f(A,B) ->[A|B].

eval_f([H|T],A) ->
    case catch H(A) of
	{'EXIT',{function_clause,_}} -> eval_f(T,A);
	B      -> B
    end;
eval_f([],A) -> 
    empty.


%% Example
fun1(blip) -> foo.
fun2({help}) -> hohoho.

base_f() ->[fun fun2/1,fun fun1/1].

test1(A) -> eval_f(base_f(),A).

test2(A) -> 
   Extended_f = extend_f(fun(blip) -> boo end, base_f()),
   eval_f(Extended_f, A).
------
Running the example:
21> extend_test:test1(blip). 
foo
22> extend_test:test2(blip).
boo
23> extend_test:test1({help}).
hohoho
24> extend_test:test2({help}).
hohoho
25> extend_test:test2(snip).  
empty


I can understand that there are some flawns, you have to do
stuff in runtime, and naming is maybe not consistent with OO,
but maybe something to build on...

Regards
Mikael





More information about the erlang-questions mailing list