How to specify(force) the Data Type of fields in records.

Papa Tana papa.tana101@REDACTED
Tue Jul 7 21:21:42 CEST 2020


Hi All,

I would like to create a Record, with Strict Data Type as Input:
		- name must be a string
		- price must be an atom
		- address must be an Ip address
		- rest can be anything
		
Here is my code:

-module(mym).

-type personal_name() :: string().
-type personal_price() :: atom().
-type personal_address() :: inet:ip_address().

-record(computer, {
					name :: personal_name(),
					price :: personal_price(),
					address :: personal_address(),
					rest }).

-export([start/0]).

start()->
	C = #computer{
					name= blaster,
					price= "55.0",
					address= <<0:8>> },
					
	{is_record(C, computer),C}.
	
When I run it:

1> mym:start().
{true,{computer,blaster,"55.0",<<0>>,undefined}}
2>

undefined is normal, but:
    * name= blaster, %% I put an atom, I want it to be accepted only if a string
    * price= "55.0", %% should accept only atom
    * address= <<0:8>>, %% shoul accept only ip_address, not binary

Could you please tell me how to specify(force) the Data Type of these
input value?

Thanks,


More information about the erlang-questions mailing list