Skip to content

Instantly share code, notes, and snippets.

@mzemel
Created October 15, 2015 17:08
Show Gist options
  • Save mzemel/a3e426963ef3b08f600e to your computer and use it in GitHub Desktop.
Save mzemel/a3e426963ef3b08f600e to your computer and use it in GitHub Desktop.
# Basic function
defmodule A do
def add(a, b) do
a + b
end
end
A.add(1, 2)
# => 3
# Guard clauses
defmodule B do
def add(a, b) when is_string(a) do
a <> b
end
end
B.add("foo", "bar")
# => "foobar"
# Combine to overload functions
defmodule C do
def add(a, b) when is_string(a), do: a <> b
def add(a, b), do: a + b
end
C.add(1,2) # => 3
C.add("foo","bar") # => "foobar"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment