Created
June 3, 2016 22:23
-
-
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!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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