marick

marick

About how Phoenix renders iodata rather than strings

As a new Phoenix user, I was happy to discover that Phoenix doesn’t render strings but something rather like Erlang iodata. That lets me use a more builder-style approach for creating chunks of HTML pages - but without having to use elaborate infrastructure. Seems to be working nicely so far.

In any case, I thought I might write up a blog piece about what I now understand about Phoenix rendering and iodata. As a newbie, I’ve probably gotten things wrong. Corrections welcome here or at http://blog.roundingpegs.com/taking-advantage-of-phoenix-rendering-and-iodata-part-1/

Most Liked

OvermindDL1

OvermindDL1

Nice, although one thing to note is that although eex accepts ‘nil’, it should not really ever be used. Returning an empty list of [] is always a better approach than returning nil as it then allows it to be embedded further without worry. I consider nil’s returned from a function that could ever possibly end up going to a template as a bug.

Other than that your article looks like a fairly accurate high level view. :slight_smile:

michalmuskala

michalmuskala

I honestly don’t think this is a good idea. Erlang uses nil to mean empty list only because that’s the naming in lisp. But I don’t think there’s a strong technical reason for it.

Erlang itself most often uses the atom undefined for uninitialised state, so I think using an atom for this purpose in elixir makes the most sense.

josevalim

josevalim

Creator of Elixir

[] is not Erlang’s nil. That notation is used only in the abstract syntax tree and in Erlang code nil has exactly the same as nil in Elixir.

Plus [] has the notion of cardinality, which nil is devoid of. Given Elixir has both collections and base types, I believe it is important to represent nothingness without a notion of cardinality built-in.

Last Post!

josevalim

josevalim

Creator of Elixir

Exactly. Such happens because empty lists are common and worth optimizing for but it does not imply they are semantically meant to be nil at the language level.

Where Next?

Popular in Discussions Top

AstonJ
Are there any Elixir or Erlang libraries that help with this? I’ve been thinking how streaming services like twitch have exploded recentl...
New
und0ck3d
Hello everyone! A few days ago I’ve created a topic here about how people were creating CMSs with Elixir and Phoenix. I’ve been studying...
New
PragTob
Hey everyone, this has been brewing in my head some time and it came up again while reading Adopting Elixir. GenServers, supervisors et...
New
PragTob
Hello everyone, I know we had quite some threads (read through lots of them) about background job processing but it remains a hotly deba...
New
lucaong
Hello Elixir and Nerves community, I have been working for a while on an open-source embedded key-value database for Elixir, that I call...
230 14350 124
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
fireproofsocks
I’ve been working on an Elixir project that has required a lot of scripting. I usually reach for Elixir because I like it more (and in th...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement