Entries vs Tagged tuples in the public api

Is it common practice to return records from query functions? If not, what are the disadvantages?

Let's say I have a query service that can return some cars and they are currently handed over or not.

My record for a car looks something like this:

-type rent_state:: rented | available.
-record(car, {make::string(), year::integer(), color::string(), state::rent_state()}).

If I then had a request for cars, is it wise to do this to return this record?

-type car::#car{}.
-spec cars() -> [car()].

or should I create a tagged type instead?

-type car()::{ {make, string()}, {year,integer()}, {color,string()}, {state,rent_state()}}).

I want to make an API that is nice to use for others. Any advice is appreciated.

+3
source share
1 answer

I like to use records locally in the module, but I believe that sharing them between applications runs in a few warts:

  • REPL
  • , , , drozzy_vehicles_car - ,
  • , , .

, , - - .

, , .

:

Car = #{ make => "foo", year => 2016, color => "red", state => rented}.

Accessors:

1> Car = rentals:get_car().
{car,"foo",2016,"red",rented}  %% it actually a record, but we don't expose that!
2> rentals_car:year(Car).
2016
3> rentals_car:state(Car).
rented
4> % etc
+3

Source: https://habr.com/ru/post/1648411/


All Articles