Is Erlang Object-Oriented?

Viewed 11904

Message-passing is a fundamental part of Erlang. Alan Kay has argued that message-passing is a concept more important than objects in his view of object-oriented programming (and he "invented" the term!).

Can Erlang be considered an object-oriented programming language (à la Smalltalk)?

7 Answers

There’s a great quote by Joe Armstrong, the creator of Erlang:

The problem with object-oriented languages is they’ve got all this implicit environment that they carry around with them. You wanted a banana but what you got was a gorilla holding the banana and the entire jungle.

Would you call this code "object oriented" ?

main() ->

    Clock1 = clock:constructor() , 

    H1 = Clock1:getTime() ,

    timer:sleep( 1500 ) , 

    H2 = Clock1:getTime() ,

    Clock1:setTime( 100 ) , 

    timer:sleep( 1500 ) , 

    H3 = Clock1:getTime() ,

    Clock1:stop( ) 

        . % main ()

This the implementation of this "Erlang class" clock.

%% --------------------------------------------------------
%%
%% clock.erl
%%
%% --------------------------------------------------------

%% ------------------------------------------------
%% ------------------------------------------------
-module( clock ).
-export( [ constructor/1, constructor/0, loop/1, 
           getTime/1, setTime/2, stop/1,
           ticTac/1
         ] ). 

%% ------------------------------------------------
%% 
%% ------------------------------------------------
-record( clock, { time } ).

%% ------------------------------------------------
%% 
%% ------------------------------------------------
constructor() -> constructor( 0 ) . % ()

%% ------------------------------------------------
%% 
%% ------------------------------------------------
constructor(H) ->
    %%io:fwrite( "\nclock:constructor()\n" ) ,

    LoopPid = spawn( ?MODULE, loop, [ #clock{ time=H } ] ) ,

    TicTacPid = spawn( ?MODULE, ticTac, [ LoopPid ] ) ,

    {clock, LoopPid}
    . % ()

%% ------------------------------------------------
%% 
%% ------------------------------------------------
ticTac( LoopPid ) ->
    timer:sleep(1000) ,
    LoopPid ! ticTack ,
    ticTac( LoopPid ) 
        . % ()

%% ------------------------------------------------
%% 
%% ------------------------------------------------
loop( State ) ->

    %% io:fwrite( "\nclock:loop()\n" ) ,

    receive
        {Pid, get} -> Pid ! State#clock.time ,
                      loop( State )  ;

        {set, H} -> loop( #clock{ time=H } ) ; 

        stop -> ok ;

        %%after 1000 ->  % creo que se reinicia tras cada recepción
        ticTack ->
                io:fwrite( ".\n" ) ,
                loop( #clock{ time=State#clock.time+1 } )
        end 

        . % ()

%% ------------------------------------------------
%% helper
%% ------------------------------------------------
getTime( {clock, PidLoopClock} ) ->

    PidLoopClock ! {self(), get} ,
    receive
        Time -> Time
        end

    . % ()

%% ------------------------------------------------
%% helper
%% ------------------------------------------------
setTime( H, {clock, PidLoopClock} ) -> 

    PidLoopClock ! {set, H} 

    . % ()

%% ------------------------------------------------
%% helper
%% ------------------------------------------------
stop( {clock, PidLoopClock} ) -> 

    PidLoopClock ! stop

    . % ()

%% --------------------------------------------------------
%% --------------------------------------------------------
%% --------------------------------------------------------
%% --------------------------------------------------------
Related