<html><head></head><body><div style="color:#000; background-color:#fff; font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:16px"><div id="yiv0399041759"><div id="yui_3_16_0_ym19_1_1474984041665_399010"><div style="color:#000;background-color:#fff;font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:16px;" id="yui_3_16_0_ym19_1_1474984041665_399009"><div dir="ltr" id="yiv0399041759yui_3_16_0_ym19_1_1474984041665_393644"><span id="yiv0399041759yui_3_16_0_ym19_1_1474984041665_393645">I am not comparing implementation details but abstract concepts, what is the purpose of such a type. The underlying purpose of records and Elixir Structs are fairly similar, the main<br clear="none">difference is that Elixir early on had huge problems with the way records are implemented in Erlang (and still problems exist) so it was hard to port over Erlang records into Elixir (same reason why Erlang macros cannot be ported cleanly). Thus they created Structs which fit into Elixirs way of doing things much better and solve the same problem records solve. You should not be using records in Elixir unless you want headache.<br clear="none"><br clear="none">As I defined in brackets, I mean the general OOP definition. The general definition of OO is objects DEFINE how you manipulate data. For example you have a bike object with the turn_pedals function, which mutates data belonging to the bike object such as its position in the world after the pedals turned. <br clear="none"><br></span></div><div dir="ltr" id="yiv0399041759yui_3_16_0_ym19_1_1474984041665_393644"><span id="yui_3_16_0_ym19_1_1474984041665_399573">IMO the correct way to manipulate data is to EXPRESS your intention. A poor example of this is you have functions that take data and return data. <br><br clear="none">turn_pedals(BikeSpeed, RiderStrength) -><br clear="none"> {ok, 2}.<br><br>You pass the speed and strength to the function. Now perform the calculations and return the distance moved. Now you are responsible for doing something knowing that if the the rider with this strength were to turn the bikes pedals, it would move 2 units. This way your structure does not pollute the expression. Should the structure change, your expression will still be perfectly valid and working.<br clear="none"><br clear="none">The hard and painful way IMO is like this:<br clear="none"><br clear="none">turn_pedals(BikeStruct, RiderStruct) -><br clear="none"> {ok, NewBikeStruct, NewRiderStruct}.<br clear="none"><br clear="none"><br clear="none">My point is that the second example is the OOP approach, where you change data defined as objects. The first approach is more functional where there is no relationship to what happens inside the expression to the object defining the data.<br><br>The example is bad because it does not cover the case of when the Bike or Rider objectified data changes. But this is a ongoing exercise with many correct approaches. I find it works to keep functions that pull out data and write data to the objectified data all in 1 module. So if you change the structure of the objectification, you wont have to go and hunt down references in 20+ source files. <br><br clear="none">I think this answers the last question as well.<br clear="none"><br clear="none"><br clear="none"></span></div> <div class="yiv0399041759qtdSeparateBR"><br clear="none"><br clear="none"></div><div class="yiv0399041759yqt8219539820" id="yiv0399041759yqt84828"></div></div></div></div><div class=".yiv0399041759yahoo_quoted"> <div style="font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:16px;"> <div style="font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:16px;"> <div dir="ltr"><font size="2" face="Arial"> On Saturday, October 1, 2016 2:33 PM, Uniaika <uniaika@crypto-keupone.eu> wrote:<br clear="none"></font></div> <br clear="none"><br clear="none"> <div class="yiv0399041759y_msg_container">There are two things I don't understand in your message:<br clear="none"><br clear="none">The first is that Elixir would be backed, or at least similar to<br clear="none">Erlang's records, which isn't quite true, since Elixir's Structs are<br clear="none">built with maps. Maybe you saw something deep in the source code that<br clear="none">made you understand the true nature of structs, but for the moment I'm<br clear="none">keeping the definition provided by the official documentation.<br clear="none"><br clear="none">The second thing is the so-called OO nature of Structs. You see them as<br clear="none">objects. If I refer to a definition given by Joe Armstrong (who will<br clear="none">maybe forgive me to bring his article in this context, or maybe not), an<br clear="none">OO object is a cluster of data structures and functions.<br clear="none"><br clear="none">So now I'm asking you: How do you manage to access functions from a data<br clear="none">structure that doesn't contain any? I'd be more than happy to be<br clear="none">revealed the great truth beyond the truth concerning structs.<br clear="none"><div class="yiv0399041759yqt1949549996" id="yiv0399041759yqtfd00634"><br clear="none">On 10/01/2016 06:39 PM, Vans S wrote:<br clear="none">> That is bad design and advice IMO. Structs in elixir define object<br clear="none">> orientation, functional programming is about data, everything is data.<br clear="none">> The tuple being a core building block.<br clear="none">> <br clear="none">> When you define your application using structures, you have just added<br clear="none">> an extra layer of inference. Now in most cases that extra layer will<br clear="none">> just get in your way, providing no reasonable benefit.<br clear="none">> <br clear="none">> One of the biggest enlightenments and problems Erlang developers learn<br clear="none">> early on is the record. First the record is pretty much a Struct in<br clear="none">> Elixir, with some insignificant differences. Now most developers would<br clear="none">> do this; define rigid records like 'User', 'Location', 'Job' with<br clear="none">> default values and start writing expressions to work with them. Before<br clear="none">> the developer knows it, the record is being used throughout multiple<br clear="none">> modules across the entire code base. Everything is unit tested and<br clear="none">> static analysis reports the code is 100% excellent, not a single flaw.<br clear="none">> <br clear="none">> Now the developers code is running in production and its working great! <br clear="none">> <br clear="none">> But now a problem comes along, the job the code does has changed,<br clear="none">> storing the email_verification_token inside the 'User' record is no<br clear="none">> longer valid, and a new record is created called 'Validation' that<br clear="none">> houses email_verification_token with sms tokens and other validations.<br clear="none">> Sure no problem, well defined structure is awesome!<br clear="none">> <br clear="none">> The entire code base was written rigidly following the spec defined by<br clear="none">> the records (Struct), as soon as we want to make a change now multiple<br clear="none">> modules need to be rewritten. Now this is not a problem, a day can be<br clear="none">> expent to rewrite/replace 20 modules that use that record. Done,<br clear="none">> rewritten! <br clear="none">> <br clear="none">> Now the developers code is running in production and its failing :(<br clear="none">> <br clear="none">> Turns out by simply replacing text in 20 modules something was missed<br clear="none">> that is producing undefined behavior now. The project is rolled back in<br clear="none">> production to the previous version and the entire development process<br clear="none">> starts all over, trying to figure out where the bug is that was<br clear="none">> introduced from the simple record change.<br clear="none">> <br clear="none">> <br clear="none">> Tl; Dr: Never rely on records (Elixir Structs) across multiple modules,<br clear="none">> always write expressions that manipulate DATA, never write expressions<br clear="none">> that manipulate OBJECTS (oop definition).<br clear="none">> <br clear="none">> <br clear="none">> On the count of maps anything used as a key is optimal, there is no<br clear="none">> limitations. Maps are a great flexibility and there is no one right way<br clear="none">> to use them. An example of a useful map key:<br clear="none">> <br clear="none">> DataChannelLookup#{}<br clear="none">> maps:put({peer1, peer2}, Channel, DataChannelLookup)<br clear="none">> maps:put({peer2, peer1}, Channel, DataChannelLookup)<br clear="none">> maps:put({peer2, peer3}, Channel2, DataChannelLookup)<br clear="none">> maps:put({peer3, peer2}, Channel2, DataChannelLookup)<br clear="none">> <br clear="none">> Channel2 = maps:get({peer2, peer3}, DataChannelLookup)<br clear="none">> <br clear="none">> <br clear="none">_______________________________________________<br clear="none">erlang-questions mailing list<br clear="none"><a rel="nofollow" shape="rect" ymailto="mailto:erlang-questions@erlang.org" target="_blank" href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br clear="none"><a rel="nofollow" shape="rect" target="_blank" href="http://erlang.org/mailman/listinfo/erlang-questions">http://erlang.org/mailman/listinfo/erlang-questions</a><br clear="none"></div><br clear="none"><br clear="none"></div> </div> </div> </div></div></body></html>