Skip to content

Instantly share code, notes, and snippets.

@jamesarosen
Last active December 19, 2015 18:08
Show Gist options
  • Select an option

  • Save jamesarosen/5996196 to your computer and use it in GitHub Desktop.

Select an option

Save jamesarosen/5996196 to your computer and use it in GitHub Desktop.
Some questions and notes about Elixir

Elixir Notes

Defining Records with Shared Values

In JavaScript, this is a big no-no:

function Set() {
}
Set.prototype.content = [];

The problem is that when you create multiptle Set objects, their content property points to the same object. When one modifies that object, all the other instances will be updated.

Thus, I was very surpsied when I saw

defrecord Company, [
  name: nil, employees: HashDict.new, autoid: 1
] do

Given my background, I was really worried about that HashDict.new being shared across all Companys. Of course, in Elixir, that's not a problem. That record is immutable. As soon as a company wants to modify it, it has to replace it with a new one!

Elixir Questions

Matching on the Right

This works:

iex> {x, 2} = {1, 2}
{1, 2}
iex> x
1

I would think that Elixir would be able to do the inverse:

iex> {1, 2} = {x, 2}
{1, 2}
iex> x
1

Or even on both sides:

iex> {x, 2} = {1, y}
{1, 2}
iex> x
1
iex> y
2

Why can't it figure that out?

Some Built-Ins are Extensible

elixir-lang/elixir#1424

No Unicode In Source?

I wanted to do this

defmodule EnumSyntax do
  def elenum, do: member?(enum, el) end
end

or even

defmoule EnumSyntax do
  defmacro elenum do
    quote do: unquote(el) in unquote(enum)
  end
end

But Elixir complains, (SyntaxError) iex:16: invalid token: ∈ enum do.

Why is String Concatenation Different from Interpolation?

defrecord Person, name: nil

defmodule Binary.Chars.Person do
  def to_binary(Person[name: name]), do: name
end

sally = Person.new([name: "Sally"])

"Hello, #{sally}" # "Hello, Sally"
"Hello, " <> sally # ArgumentError

cf https://groups.google.com/forum/#!topic/elixir-lang-talk/jV-OmI8S4rU

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment