Records, encapsulation and optional elements
Alex Peake
apeake@REDACTED
Tue May 21 16:57:29 CEST 2002
Thank you so much. Much more elegant!
(I also got useful suggestions from Ingela Anderton - similar to your second
solution - and Willem Broekema).
It seems, then, that there is a trade-off between the compact, elegant code
solutions that you suggest and my clumsy attempt:
* Your solutions involve copying the record several times before completion
* My solution looks ugly (and creates lots of intermediate variables)
Is that a reasonable assessment?
Alex
BTW, I assume the copying cost is not significant?
> -----Original Message-----
> From: Vladimir Sekissov [mailto:svg@REDACTED]
> Sent: Tuesday, May 21, 2002 2:24 AM
> To: apeake@REDACTED
> Cc: erlang-questions@REDACTED
> Subject: Re: Records, encapsulation and optional elements
>
>
> 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