开发者

how to subtype records with erlang's compiler approval?

I am trying to define a record #a and a record #b such that #b extends #a, so that I can treat #b (and other subtypes of #a) as #a in some situations. The compiler, though, does not like it and keeps trying to guess the record type based on the first access I开发者_如何学运维 make to it. The following code triggers the warning.

-module(sandbox).  
-record(a,{alfa,beta}).  
-record(b,{alfa,beta,gama}).  
-export([test/0]).
test() ->  
    A = #b{alfa=1,beta = 2,gama=3},  
    self()!{msg,A},  
    receive  
    {msg,Msg} ->  
        Alfa = Msg#b.alfa,
        Beta = Msg#b.beta,
        case is_record(Msg,b) of
            true ->
                Gama = Msg#b.gama;
            false->                  %% Warning. Erlang assumes that Msg is a #b and therefore this will never match.
                Gama = []
        end
end,
io:format("~p ~p ~p",[Alfa,Beta,Gama]).


test1() ->
A = #b{alfa=1,beta = 2,gama=3},
self()!{msg,A},
receive
    {msg,Msg} ->
        Alfa = Msg#a.alfa,
        Beta = Msg#a.beta,
        case is_record(Msg,b) of
            true ->                  %% Warning. Erlang assumes that Msg is an #a, and therefore this will never match.
                Gama = Msg#b.gama;
            false->
                Gama = []
        end
end,
io:format("~p ~p ~p",[Alfa,Beta,Gama]).

Is there anyway I can use this subtyping and make the compilation warning go away? Thanks.


I don't think this can work, because "-record(a,{alfa,beta})." is sort of a template for "{a, alfa, beta}" and "-record(b,{alfa,beta,gama})." results in a tuple "{b,alfa,beta,gama}".

Have a look at http://erlang.org/doc/getting_started/record_macros.html#id66845 , please...


If you want inheritance in Erlang use module inheritance:

-module(inh).
-extends(base).

Also useful "OOP" feature in Erlang is parameterized modules:

-module(param, [Id, Name]).
-compile(export_all).
id() -> Id.
name() -> Name.

and use it:

P = param:new(1, stas).
P:id(). % returns 1
P:name(). % returns stas

Maybe combination of these features helps you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜