Elixir v1.6 released: code formatter, dynamic supervisors, and more
elixir-lang.org
elixir-lang.org
How was the default style decided? And very importantly, what's the forum for users to bikeshed endlessly about how it should be different?
More seriously, as someone who's literally updating a bit of code to use a :simple_one_for_one supervisor right now - is the new DynamicSupervisor more about naming, so people understand it better, than semantics? I was under the impression that :simple_one_for_one, :rest_for_one, etc, were all standard erlang OTP supervisor strategies and that Elixir mostly deferred to them. Is it still using the same erlang semantics under the hood?
Oh, and edited to add: does that mean using `:simple_one_for_one` with a `Supervisor` is now deprecated? If so, what's involved in transitioning to the new approach?
I too like 80 max chars since it lets me fit 3 side by side code windows. From what I gathered this is configurable at a project level.
To me it seems like a huge step up from having to use a tricked out rubocop/flake8 config to detect issues while you have to manually fix them.
Just knowing I never have to deal with formatting again because Jose and gang put a crazy amount of effort into ensuring it does the right thing is really nice.
> A `.formatter.exs` file can also be defined for customizing input files and the formatter itself
https://github.com/elixir-lang/elixir/blob/master/lib/mix/li...
It is mostly about naming, the semantics are the same. The DynamicSupervisor has a new feature called max_children that Erlang doesn't but that's it.
> Oh, and edited to add: does that mean using `:simple_one_for_one` with a `Supervisor` is now deprecated? If so, what's involved in transitioning to the new approach?
It will be eventually, probably on v1.8. Good question about transitioning. We should have some docs on that. I will work on it.
https://gist.github.com/christhekeele/76c3e37cb9082274f52f79...
While not the final spec per his PR, a really good look at the internals nonetheless.
I tried my hand at a pure-Elixir guard expansion in an earlier PR, documenting my thoughts as I went: https://github.com/elixir-lang/elixir/pull/5854
After José gave me some direction in response to that, I produced the ultimate PR: https://github.com/elixir-lang/elixir/pull/5857
It simply relies on calling the :elixir_expand.expand function that the compiler uses, with a guard context—so the defguard implementation is always up-to-date with what the compiler allows in guards. Waaay more elegant than munging AST myself.
Funnily enough, the tests I wrote for defguard actually uncovered some missing assertions in that function. Some invalid expressions would make it past that step and return a different error much deeper in. It's kinda cool I indirectly contributed to the compiler.
I took on the feature mostly because over 4 years ago I'd made a gist to do exactly that, minus the validation, and when I found the issue on github for it, it gave me a lot of pleasant closure to get it merged in! https://gist.github.com/christhekeele/8284977/revisions#diff...
Pre commit hook: https://github.com/jasongoodwin/elixir-mix-format-pre-commit...
Emacs format current file: https://github.com/jasongoodwin/emacs-elixir-formatter
Generally the formatter is good, but we've noticed a couple cases where it would be nice to override
EG:
it would be nice to have the join and on statements on the same line so you can see as the reader what relates to what
u in Db.User,
distinct: u.id,
join: c in Db.Comment,
on: u.id == c.user_id,
join: p in Db.Profile,
on: u.profile_id == u.id,
join: x in Db.SomethingElse,
on: x.profile_id == u.id,
join: y in Db.AnotherThing,
on: y.profile_id == u.id,
...I can see why it's like that, but it would be nice to be able to override it.
Also the formatter requires () so you end up with ecto code differing from the styles recommend in the docs
schema "thingy" do
field(:name, :string)
field(:abbreviation, :string)
end
instead of schema "thingy" do
field :name, :string
field :abbreviation, :string
end
Anyway, still useful and good for the team.One of the big advantages I have experienced from the Golang formatter is that you cannot configure it, period. This took me a little bit off getting over myself, but eventually the fact that every single Golang codebase adheres to these rules is so nice. Plus you don't have to think about it, ever.
In this case I think it would be best if Ecto just recommends the new default style in their docs.
Would it be possible to have that be trivially configurable to hook into save-buffer? That would seem to be the cleanest way to use it.
If anyone starts learning Elixir now: I can recommend using VSCode with ElixirLS extension. Also: pragprog books, Sasa Juric's book. If you like video tutorials I've heard good things about PragDave's course.
(1) Philosophically, Erlang borrows a lot from Prolog while Elixir borrows a lot from Ruby. For some programmers (dilettantes like myself included), that relative familiarity is a fairly big win. Go doesn't have the same "weird syntax" drawback; it's pretty easy for anyone familiar with C-like languages to pick up the basics virtually on sight.
(2) Technically, Elixir is another language for the Erlang VM, like Scala and Clojure are other languages for the Java VM. But Go doesn't have a VM; its compiler produces native code. So there's no way to produce something that's strictly comparable. You could write a preprocessor that translates an entirely new syntax into Go, but that wouldn't be Go's Elixir, it would be Go's CoffeeScript.
As someone terrified of lower level languages (I'm a somewhat JR dev, used to working with fun things like Python, Ruby, Elixir and JS) this would kind of be really nice. Mostly maps.
Have: http://havelang.org/ with generics
Godzilla: https://github.com/jingweno/godzilla ES2015 to Go
I didn't try any of them, nor Go itself.