Defmodule one-liner?

Is it possible to define a module with a function as a one-liner?

I’m trying out iex and it would be nice (I think) to be able to do (something like this):

ex(6)> defmodule Test do world, do: “it works!” end
** (SyntaxError) iex:6: unexpected token: end

Is this possible with another syntax?

defmodule Test, do: def world, do: "it works!

I wasn’t even close… That works fine, thanks Christopher!

You can also do

defmodule Test do def world do "it works!" end end

My guess is that the problem arises when you mix the different ways of expressing do, do ... end and , do:

1 Like

By mixing keyword-styles, you are confusing the parser. By explicitly spreading some parens you can help it being back on the track:

iex(1)> defmodule Test do def world, [do: nil] end
{:module, Test, ...