I’m trying to learn Elixir and write it using IntelliJ IDEA and the plugin. But after using mix new to create a project and create the project from existing source in IntelliJ IDEA, I’m stuck at making the program run.
Of course, using iex and specify the .ex file, it will run, but what I’m trying to set up is like an entry point that IntelliJ IDEA will directly run after I press Shift+F10. For example, if under the /lib directory, inside the example.ex I write a function called “main” that would call other functions, how can I set up IntelliJ IDEA to directly run that function? I have searched the Internet but most are using iex anyway. If anyone knows how to properly set up the configuration or some beginner-friendly guides, I’ll be grateful. Thanks.
Hey @deadshot465! Welcome to the forum!
Typically Elixir programs are long-running applications such as web-servers or databases which don’t have a
main function, instead they have a supervision tree which also serves as a tool for introducing concurrency and durability to the application. The official guide to OTP and mix may be useful here -> https://elixir-lang.org/getting-started/mix-otp/introduction-to-mix.html
If you wish to write an Elixir program that performs one task and then shuts down (for example a compiler or CLI tool) you may want to look at escripts: https://hexdocs.pm/mix/master/Mix.Tasks.Escript.Build.html
What kind of program are you writing?
Thank you for the reply!
I’m still trying to learn Elixir so currently I’m not writing complex applications. Probably a function that receives inputs, and then depending on the input, dispatch to several simple functions to do simple tasks or calculations, in order to make myself more familiar with Elixir syntax.
I think Escript is similar to what I’m finding, but I have yet to successfully run the program directly from IntelliJ IDEA. Don’t know how to properly set up the configurations…
I think often these sort of programs would be run either via unit tests (with
mix test) or by calling the function from within iex (start iex in a terminal using
iex -S mix and then running
recompile in iex each time you make changes before calling your function).
I would likely just keep a terminal open to run these commands in but it seems you can assign shortcuts in IDEA like so: https://www.jetbrains.com/help/idea/2016.1/external-tools.html
It seems to me that you are a little confused as I was when I started to learn Elixir… So do yourself a favour and learn from a good resource
It made all the difference for me to choose Elixir for Programmers video course, because my background is Object Orientated languages and switching to Functional Programming is not easy, but @pragdave excels in making this transition so easy :).
I strongly believe that without this course I would still have my brain wired to the way of coding in Object Orientated languages.
I’m not using IntelliJ, but rather RubyMine with the Elixir plugin. Perhaps what you are looking for is what JetBrains calls a ‘Run Configuration’. If you have installed the plugin and configured the Elixir SDK, then you can create a Mix Run configuration from the Run menu and then the ‘Run…’ menu item. Select
Elixir Mix as the template, and then enter the arguments (in my case
phx.server, since I’m running a Phoenix server). You’ll then have that in the Run dropdown in the toolbar where you can click the green arrow to run, or the debug icon to run it with the debugger. (see screenshots below). Hope that helps.
To run simple scripts (for learning purposes), I use a much simpler approach. I create a shell script runner and run a script text with the script text
elixir lib/script.exs. and it works!