Skip to content

Instantly share code, notes, and snippets.

@elbow-jason
Created July 20, 2016 18:53
Show Gist options
  • Save elbow-jason/94b21955f6e76d35294f818408dc20f8 to your computer and use it in GitHub Desktop.
Save elbow-jason/94b21955f6e76d35294f818408dc20f8 to your computer and use it in GitHub Desktop.
defmodule Something do
defstruct [
material: nil
]
def get_material(%Something{material: material}) when material |> is_function do
material.()
end
def get_material(%Something{material: material}) do
material
end
end
iex(1)> defmodule Something do
...(1)> defstruct [
...(1)> material: nil
...(1)> ]
...(1)>
...(1)> def get_material(%Something{material: material}) when material |> is_function do
...(1)> material.()
...(1)> end
...(1)> def get_material(%Something{material: material}) do
...(1)> material
...(1)> end
...(1)> end
{:module, Something,
<<70, 79, 82, 49, 0, 0, 10, 36, 66, 69, 65, 77, 69, 120, 68, 99, 0, 0, 0, 251,
131, 104, 2, 100, 0, 14, 101, 108, 105, 120, 105, 114, 95, 100, 111, 99, 115,
95, 118, 49, 108, 0, 0, 0, 4, 104, 2, ...>>, {:get_material, 1}}
iex(2)> %Something{material: "glass"}
%Something{material: "glass"}
iex(3)> %Something{material: "glass"} |> Something.get_material
"glass"
iex(4)> %Something{material: fn -> "metal" end} |> Something.get_material
"metal"
iex(5)>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment