3 ms·
I really want scripting in Elixir to be better - and the addition of Mix.install() is a great step forward - but there are still a lot of awkward elements. Elix
by aeturnum 3y ago
I really want scripting in Elixir to be better - and the addition of Mix.install() is a great step forward - but there are still a lot of awkward elements. Elixir will require you to be in a module to define a function and then respect module namespaces when calling them - which makes sense in a normal context but I find it to be annoying while scripting. Likewise I don't think dialyzer, the Elixir / Erlang static type analyzer, will run on script files.
That said I think the underlying lack of data mutability really increases the potential for code reuse and scripting.
- jolux 3y agoyou can define functions outside of a module using fn blocks
- skndr 3y agoThere's also defining a module and then `import`ing it.
- throwawaymaths 3y agoThere's good odds it will come to elixir, since support just landed in Erlang
- erszcz 3y agoTo be specific, it has landed in the Erlang shell with Erlang/OTP 26. Defining inline functions in source code, on the other hand, has been possible for years (like 20+ years).
- thibaut_barrere 3y agoAgreed, I find this a bit annoying too. Makes me create a module earlier than I'd have wanted (but not too much trouble though). One can still reuse code with "Code.require_file", which is nice to have.
- jnsaff2 3y agosum = fn (a, b) -> a + b end sum.(2, 3) Maybe a but clunky but no module required.
- lawik 3y agoMostly gets really clunky for recursive functions.
- nivertech 3y agoI don't see a problem with defining modules in Elixir scripts. It was a huge annoyance in Erlang though. Even scripts require some structure and code quality. I organize my bash scripts, otherwise they quickly become unmanagable. If you dislike typing long module names, and for some unknown reasons won't use short alias (alias My.Long.Module.Name, as: M) or import. Then the best workaround is to use "M" as a module name: #/usr/bin/env elixir ... defmodule M do ... def f, do: ... ... end ... M.f() Anyway, I think there was Joe Armstrong's blog post, where he said that modules are just containers for functions. The way we decide which module to put a function is completely arbitrary. Sometimes it's very hard to decide, or maybe the same function could equally belong to several modules. I designing my toy programming language where functions do not nelong to modules or namespaces, but you can query them based on metadata like tags/types/docs/etc.
- sandbags 3y agoIn the Ruby language you are implicitly in the context of the Main object (IIRC). If scripting was it's own context then something like this could perhaps work for Elixir. You're already in a module context implicitly.