Hal9000

Hal9000

Serialization (Behaviours and protocols)

I’ve been thinking about the topic of behaviours and protocols for several weeks,
basically a cycle of “think, discuss, code, take notes, repeat.”

Someone suggested that serialization would be a good example of something
to implement with a behaviour; someone else said that it was a natural example
for a protocol.

I think it depends on what you mean. Here is my very simple example of a
Serializable behaviour. It couldn’t be rewritten as a protocol, really. But
maybe looking at the problem differently, it could be? After all, simply saying
“serialization” doesn’t fully define the problem.

Still seeking good examples of a custom-defined behaviour and a protocol
to show their similarities and differences.

defmodule Serializable do
  @callback load(String.t) :: any
  @callback dump(any) :: String.t
end


defmodule MyCollection do
  defstruct [:foo, :bar]

  def new(x, y) do
    %MyCollection{foo: x, bar: y}
  end

  @behaviour Serializable

  def load(str) do
    [x, y] = String.split(str, "\n")
    MyCollection.new(String.to_integer(x), String.to_integer(y))
  end

  def dump(item) do
    "#{item.foo}\n#{item.bar}"
  end

end


x = MyCollection.new(3,5)

dumped = MyCollection.dump(x)

y = MyCollection.load(dumped)

First Post!

imetallica

imetallica

I would implement as a Protocol just because of one reason: you can extend the serialization to user defined types, for example.

Where Next?

Popular in Discussions Top

jswny
I would like to better understand what the advantages/disadvantages of umbrella applications are compared to structuring your app as as s...
New
arpan
Hello everyone :wave: Today I am very excited to announce a project that I have been working on for almost 3 months now. The project is...
New
AstonJ
If a newbie asked you about Phoenix Contexts, how would you explain the basics to them? Feel free to be as concise or in-depth as you li...
New
ejpcmac
I have discovered Nix last month and I am currently on my way to migrating to it—both on macOS at home and the full NixOS distrubution at...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
rower687
Hi all, I’ve been reading a lot about the “let it crash” term and how supervising processes and the whole messaging passing make an elixi...
New
jer
I’ve been using umbrellas for a while, and generally started off (on greenfield projects at least) by isolating subapps based on clearly ...
New
cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
Qqwy
Looking at the stacks that existing large companies have used, WhatsApp internally uses Mnesia to store the messages, while Discord uses ...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54250 245
New

Other popular topics Top

Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54250 245
New

We're in Beta

About us Mission Statement