Skip to content

Instantly share code, notes, and snippets.

@henrik
Created June 3, 2016 22:23
Show Gist options
  • Save henrik/e60835c2959e4832e8022d5560f5244c to your computer and use it in GitHub Desktop.
Save henrik/e60835c2959e4832e8022d5560f5244c to your computer and use it in GitHub Desktop.
Silly experiment with a type-y syntax for Elixir method definitions. Not good for anything!
defmodule Lab do
defmacro deft({name, _, [{:::, _, [argthing, {_, _, [:Integer]}]}]}, do: block) do
quote do
def unquote(name)(arg) when is_integer(arg) do
var!(unquote(argthing)) = arg
unquote(block)
end
end
end
defmacro deft({name, _, [{:::, _, [argthing, {_, _, [:String]}]}]}, do: block) do
quote do
def unquote(name)(arg) when is_binary(arg) do
var!(unquote(argthing)) = arg
unquote(block)
end
end
end
end
defmodule Run do
import Lab
deft int_me(something :: Integer) do
IO.puts 123 + something
end
deft string_me(something :: String) do
IO.puts "hi " <> something
end
end
Run.int_me(456)
Run.string_me("type system")
#Run.string_me(456)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment