Records, encapsulation and optional elements
Vladimir Sekissov
svg@REDACTED
Tue May 21 11:23:32 CEST 2002
Good day,
Your code could be more compact:
make_attr(Name, Domain, Options) ->
Rec = #attr{name = Name, domain = Domain},
Ret = lists:foldl(fun({domLen, Val}, AccRec) ->
AccRec#attr{domLen = Val};
({domPrec, Val}, AccRec) ->
AccRec#attr{domPrec = Val};
...
({autonum, Val}, AccRec) ->
AccRec#attr{autonum = Val}
end, Rec, Options),
Ret.
or without generic functions:
make_attr(Name, Domain, Options) ->
Rec = #attr{name = Name, domain = Domain},
make_attr(Rec, Options).
make_attr(Rec, []) ->
Rec;
make_attr(Rec, [{domLen, Val}| Rest]) ->
make_attr(Rec#attr{domLen = Val}, Rest);
...
make_attr(Rec, [{autonum, Val}| Rest]) ->
make_attr(Rec#attr{autonum = Val}, Rest).
Best Regards,
Vladimir Sekissov
apeake> I am new to Erlang. Could someone please advise if the following is a
apeake> reasonable attempt at encapsulating the creation of records (with optional
apeake> elements)? Is there a more elegant way? It seems a shame that I can define
apeake> defaults (in the .hrl) and yet not use them (in an encapsulated way) in the
apeake> ..erl.
apeake>
apeake> Thanks,
apeake>
apeake> Alex
apeake>
apeake>
apeake> %% File: attr.hrl
apeake>
apeake> %%-----------------------------------------------------------
apeake> %% Data Type: attr
apeake> %% where:
apeake> %% name: A string (default is undefined).
apeake> %% domain: An atom (default is undefined).
apeake> %% domLen: An integer (default is undefined).
apeake> %% domPrec: An integer (default is undefined).
apeake> %% nullable: A boolean (default is false).
apeake> %% isPk: A boolean (default is false).
apeake> %% pkPos: An integer (default is undefined).
apeake> %% fkPos: An integer (default is undefined).
apeake> %% fkRel: A string (default is undefined).
apeake> %% fkAttr: A string (default is undefined).
apeake> %% autonum: A boolean (default is false).
apeake> %%------------------------------------------------------------
apeake>
apeake> -record(attr, {name, domain, domLen = 0, domPrec = 0, nullable = false, isPk
apeake> = false, pkPos, fkPos, fkRel, fkAttr, autonum = false}).
apeake>
apeake>
apeake>
apeake>
apeake>
apeake>
apeake> -module(attr).
apeake> -include("attr.hrl").
apeake> -export([make_attr/3, varName/1, label/1]).
apeake> -export([findNamed/2, findNamedC/1, findFromNameList/2]).
apeake>
apeake> %%-----------------------------------------------------------
apeake> %% Data Type: attr
apeake> %% where:
apeake> %% name: A string (default is undefined).
apeake> %% domain: An atom (default is undefined).
apeake> %% domLen: An integer (default is undefined).
apeake> %% domPrec: An integer (default is undefined).
apeake> %% nullable: A boolean (default is false).
apeake> %% isPk: A boolean (default is false).
apeake> %% pkPos: An integer (default is undefined).
apeake> %% fkPos: An integer (default is undefined).
apeake> %% fkRel: A string (default is undefined).
apeake> %% fkAttr: A string (default is undefined).
apeake> %% autonum: A boolean (default is false).
apeake> %%------------------------------------------------------------
apeake>
apeake>
apeake> %% Name and Domain are required arguments.
apeake> %% Options is a list of Tuples for the rest.
apeake> %% Example: [{nullable, true},{isPK, true},{pkPos,1}]
apeake> make_attr(Name, Domain, Options) ->
apeake> DomLen = case lists:keysearch(domLen, 1, Options) of
apeake> {value,{domLen, Value}} ->
apeake> Value;
apeake> _ ->
apeake> undefined
apeake> end,
apeake> DomPrec = case lists:keysearch(domPrec, 1, Options) of
apeake> {value,{domPrec, Value}} ->
apeake> Value;
apeake> _ ->
apeake> undefined
apeake> end,
apeake> Nullable = case lists:keysearch(nullable, 1, Options) of
apeake> {value,{nullable, Value}} ->
apeake> Value;
apeake> _ ->
apeake> false
apeake> end,
apeake> IsPk = case lists:keysearch(isPk, 1, Options) of
apeake> {value,{isPk, Value}} ->
apeake> Value;
apeake> _ ->
apeake> false
apeake> end,
apeake> PkPos = case lists:keysearch(pkPos, 1, Options) of
apeake> {value,{pkPos, Value}} ->
apeake> Value;
apeake> _ ->
apeake> undefined
apeake> end,
apeake> FkPos = case lists:keysearch(fkPos, 1, Options) of
apeake> {value,{fkPos, Value}} ->
apeake> Value;
apeake> _ ->
apeake> undefined
apeake> end,
apeake> FkRel = case lists:keysearch(fkRel, 1, Options) of
apeake> {value,{fkRel, Value}} ->
apeake> Value;
apeake> _ ->
apeake> undefined
apeake> end,
apeake> FkAttr = case lists:keysearch(fkAttr, 1, Options) of
apeake> {value,{fkAttr, Value}} ->
apeake> Value;
apeake> _ ->
apeake> undefined
apeake> end,
apeake> Autonum = case lists:keysearch(autonum, 1, Options) of
apeake> {value,{autonum, Value}} ->
apeake> Value;
apeake> _ ->
apeake> false
apeake> end,
apeake> #attr{name = Name, domain = Domain, domLen = DomLen, domPrec = DomPrec,
apeake> nullable = Nullable, isPk = IsPk, pkPos = PkPos, fkPos = FkPos,
apeake> fkRel = FkRel, fkAttr = FkAttr, autonum = Autonum}.
apeake>
apeake>
More information about the erlang-questions
mailing list