Records, encapsulation and optional elements

Ingela Anderton <>
Tue May 21 08:55:22 CEST 2002


I would suggest something like this:

make_attr(Name, Domain, [Option | Options]) ->
   make_attr(#attr{name = Name, domain = Domain}, Options).

make_attr(Record, {nullable, Value}, []) ->
	Record#attr{nullable = Value};

make_attr(Record, {nullable, Value}, [Option | Options]) ->
   make_attr(Record#attr{nullable = Value}, Option, Options);

make_attr(Record, {domLen, Value}, []) ->
	Record#attr{domLen = Value};

make_attr(Record, {domLen, Value}, [Option | Options]) ->
   make_attr(Record#attr{domLen = Value}, Option, Options);
....

for more information on how records work see:

http://www.erlang.se/doc/doc-5.1/doc/extensions/part_frame.html


Alex Peake wrote:
> I am new to Erlang. Could someone please advise if the following is a
> reasonable attempt at encapsulating the creation of records (with optional
> elements)? Is there a more elegant way? It seems a shame that I can define
> defaults (in the .hrl) and yet not use them (in an encapsulated way) in the
> .erl.
> 
> Thanks,
> 
> Alex
> 
> 
> %% File: attr.hrl
> 
> %%-----------------------------------------------------------
> %% Data Type: attr
> %% where:
> %%    name:  A string (default is undefined).
> %%    domain:  An atom (default is undefined).
> %%    domLen:   An integer (default is undefined).
> %%    domPrec:   An integer (default is undefined).
> %%    nullable: A boolean (default is false).
> %%    isPk: A boolean (default is false).
> %%    pkPos: An integer (default is undefined).
> %%    fkPos: An integer (default is undefined).
> %%    fkRel:  A string (default is undefined).
> %%    fkAttr:  A string (default is undefined).
> %%    autonum: A boolean (default is false).
> %%------------------------------------------------------------
> 
> -record(attr, {name, domain, domLen = 0, domPrec = 0, nullable = false, isPk
> = false, pkPos, fkPos, fkRel, fkAttr, autonum = false}).
> 
> 
> 
> 
> 
> 
> -module(attr).
> -include("attr.hrl").
> -export([make_attr/3, varName/1, label/1]).
> -export([findNamed/2, findNamedC/1, findFromNameList/2]).
> 
> %%-----------------------------------------------------------
> %% Data Type: attr
> %% where:
> %%    name:  A string (default is undefined).
> %%    domain:  An atom (default is undefined).
> %%    domLen:   An integer (default is undefined).
> %%    domPrec:   An integer (default is undefined).
> %%    nullable: A boolean (default is false).
> %%    isPk: A boolean (default is false).
> %%    pkPos: An integer (default is undefined).
> %%    fkPos: An integer (default is undefined).
> %%    fkRel:  A string (default is undefined).
> %%    fkAttr:  A string (default is undefined).
> %%    autonum: A boolean (default is false).
> %%------------------------------------------------------------
> 
> 
> %% Name and Domain are required arguments.
> %% Options is a list of Tuples for the rest.
> %% Example: [{nullable, true},{isPK, true},{pkPos,1}]
> make_attr(Name, Domain, Options) ->
>     DomLen = case lists:keysearch(domLen, 1, Options) of
> 		 {value,{domLen, Value}} ->
> 		     Value;
> 		 _ ->
> 		     undefined
> 	     end,
>     DomPrec = case lists:keysearch(domPrec, 1, Options) of
> 		  {value,{domPrec, Value}} ->
> 		      Value;
> 		  _ ->
> 		      undefined
> 	      end,
>     Nullable = case lists:keysearch(nullable, 1, Options) of
> 		   {value,{nullable, Value}} ->
> 		       Value;
> 		   _ ->
> 		       false
> 	       end,
>     IsPk = case lists:keysearch(isPk, 1, Options) of
> 	       {value,{isPk, Value}} ->
> 		   Value;
> 	       _ ->
> 		   false
> 	   end,
>     PkPos = case lists:keysearch(pkPos, 1, Options) of
> 		{value,{pkPos, Value}} ->
> 		    Value;
> 		_ ->
> 		    undefined
> 	    end,
>     FkPos = case lists:keysearch(fkPos, 1, Options) of
> 		{value,{fkPos, Value}} ->
> 		    Value;
> 		_ ->
> 		    undefined
> 	    end,
>     FkRel = case lists:keysearch(fkRel, 1, Options) of
> 		{value,{fkRel, Value}} ->
> 		    Value;
> 		_ ->
> 		    undefined
> 	    end,
>     FkAttr = case lists:keysearch(fkAttr, 1, Options) of
> 		 {value,{fkAttr, Value}} ->
> 		     Value;
> 		 _ ->
> 		     undefined
> 	     end,
>     Autonum = case lists:keysearch(autonum, 1, Options) of
> 		  {value,{autonum, Value}} ->
> 		      Value;
> 		  _ ->
> 		      false
> 	      end,
>     #attr{name = Name, domain = Domain, domLen = DomLen, domPrec = DomPrec,
> 	  nullable = Nullable, isPk = IsPk, pkPos = PkPos, fkPos = FkPos,
> 	  fkRel = FkRel, fkAttr = FkAttr, autonum = Autonum}.
> 

-- 
/m.v.h Ingela

//The highway of life is always under construction. //

 	        |\      _,,,--,,_  ,)
                /,`.-'`'   -,  ;-;;' 
               |,4-  ) )-,_ ) /\
              '---''(_/--' (_/-'

Ericsson AB - OTP team

Cellular/Mobile: +46 70 636 78 68 







More information about the erlang-questions mailing list