I know that I can create something like that
defmodule Test do
defmacro wrap(do: do_expr, else: else_expr) do
and use it like
Also I can use
rescue and other reserved words (macro?) instead of
Is it possible to define my own word (macro?) that will work same?
It’s not possible to define your own. The ones that are available are:
I found what I can do like that
defmacro wrap(do: do_expr, myown: else_expr) do
and use like
Test.wrap do: (IO.puts :abc), myown: (IO.puts :bcd)
and it will be working ok. It demonstrates that possible even pass block of code as method parameter. But it would be good if the developer could declare own keywords.
Is it planned to add possibility to declare custom keywords in Elixir?
Thanks for all replies
I think that it will be very useful to allow creating custom keywords (or other name, but with behavior described above)
But for now topic is closed. Thank you
For note, a block can be defined multiple times, so:
Or whatever else is all valid and you can handle it in your macro (I’ve done it). You can also use something like
-> (which binds very tightly) to do conditionals or whatever you want on the left and whatever else on the right, useful to split up a body like:
doSomething() -> doSomethingElse()
42 -> 6.28