16 ms·
Sketches of Elixir
- wmock 8y agoI've been learning Elixir as well and practicing my Elixir with this year's Advent Of Code. So far, I've really been enjoying writing in Elixir but if there's one thing I wish it was better at, it would be debugging in Elixir. Maybe its because I'm so used to using Chrome's developer tools, but I wish there was something as easy and simple to use like that for debugging Elixir.
- xutopia 8y agoHave you tried using IEx.pry?
- deleted 8y ago[deleted]
- pmontra 8y agoI agree with the author. Elixir and Ruby share almost only the R letter in the name and some name of modules/method/functions. They are two very different languages. A complaint of mine about Elixir is that `with` is a macro and not a statement of the language. That probably made it easier to implement but moving code inside/outside a `with` is a pain: transform the = into <- and add a comma at the end of the line. This is against programmer happiness. The feature I love most is pattern matching in function arguments. It makes the code more readable by removing almost every single case/if.
- dnautics 8y agoI think generally the issue of when to use <- and when to use -> and when to use = is one of the few things about the language that makes my head spin.
- Exuma 8y ago<- is a comprehension, its like a for loop. -> is like for everything else like case and cond = is bind operator and is only used when you want to pattern match
- dnautics 8y ago<- is used for with as well and that is not a comprehension, and in feel way more like an = pattern match. -> is used in lambdas, which is kind of like case (but not cond).
- okbake 8y ago`with` is an oddball. It's a macro but it's also a special form that has unique syntax that's not used anywhere else. It's basically the only variadic function in the language and it doesn't fit IMO. It could have been implemented without needing the same special treatment with something like: with do x <- foo() y <- bar(x) z <- baz(y) blah(z) else err -> handle_err(err) end
- pmontra 8y agoActually I'd like this with x = foo() y = bar(x) z = baz(y) blah(z) else err -> handle_err(err) end No do (uselessly verbose, it litters all the language) and = instead of <- It should be normal code and take the else branch whenever there is any error, pattern matching included. The advantage is that the code can be indented in or out a with block, without any change. Much more convenient. Elixir also have a try/catch https://elixir-lang.org/getting-started/try-catch-and-rescue.html https://elixir-lang.org/getting-started/try-catch-and-rescue... > In Elixir, we avoid using try/rescue because we don’t use errors for control flow. We take errors literally: they are reserved for unexpected and/or exceptional situations.
- PopeDotNinja 8y ago> No do (uselessly verbose, it litters all the language) and = instead of <- It should be normal code and take the else branch whenever there is any error, pattern matching included. Using '=' insead of '<-' would break how with works. I can understand why one thinks the syntax looks weird. I avoid with when possible, but sometimes it's the easiest abstraction to use.
- jimbokun 8y ago"They are two very different languages." Then you later mention "programmer happiness". And I think an emphasis on "programmer happiness" is exactly what Elixir and Ruby share, and is probably the reason Rubyists are drawn to Elixir.
- brandall10 8y agoElixir was literally written by a Rails core team member as a response to issues he had in working on enterprise Rails apps. The Phoenix framework as well was written by a long-time Rubyist, and the book they wrote together, Programming Phoenix, reads in places like a treatise on lessons learned from working with Rails. So Rubyists (I myself have been one since 2011) are drawn to it because it essentially has been billed as a successor to Rails when you need to be concerned with highly concurrent, fault-tolerant, TDD-driven web apps. Most companies who have embraced it are former Rails shops... most devs who use it are former Rails devs.
- tiglionabbit 8y agoOr because the syntax is directly inspired by Ruby.
- rs86 8y agoNoooooooo it's erlang with Ruby syntax, powerful meta programming, ad-hoc polymorphism and many other things...
- dnautics 8y ago1) I would argue that the pipe operator is *more erlangey than ruby-ey. It is crazily enough possibly the most useful syntactic sugar feature I've ever seen, especially coupled with labelled IO.inspect. I've never had to go down to the debugger, and reverting the inspect is nearly instantaneous. 2) my number one wish-that-will-never happen for elixir would be the inverse of the mutability issue, variables should be immutable by default but have a sigil or postfix (like !) that lets you make it mutable. Of course, as it is, this is trivially implementable as a credo linter feature. 3) I think generally the biggest problem with macros is the risk of nonlocal side effects in your code, and elixir does a nice job of containing it lexically.
- _asummers 8y agoYou can get mutability, you just need to be explicit with it using a GenServer or some other process abstraction. It just has to be explicit via message passing because your local node is just a special case of the overall network topology that Erlang needs to service.
- losvedir 8y ago> 2) my number one wish-that-will-never happen for elixir would be the inverse of the mutability issue, variables should be immutable by default but have a sigil or postfix (like !) that lets you make it mutable. Just to clarify for others, and as the article notes, variables aren't "mutable" in Elixir but rather can be rebound. Even rust, where - just like you desire - you have to explicitly tag something "let mut" if you want mutability, will let you re-bind the variable without that tag. I've programmed in Elixir full time now for two years and I can't recall any times accidentally shadowing a variable has bit me (but I do `mix compile --force --warnings-as-errors`) as part of my development routine, and that has caught it before for me. (Oh, unless you mean you wish Elixir had _real_ mutability. That _would_ be quite the change, as obviously no functions in the standard library support that and you have to make do with GenServers and the like. I'm not sure I'd like that as the whole language ecosystem is predicated on immutability and message passing...) While we're throwing out "one wish-that-will-never happen" ideas, though, here's mine: I'd love for Elixir to be able to automatically add guards to functions based on the type spec. That is, if you had this code: @spec foo(String.t(), MyEnum.t()) :: boolean() def foo(str, enum) do # ... end I would love it if that got compiled to essentially: def foo(str, enum) when is_binary(str) and enum in [:enum1, :enum2, ...] do # .. end In general, dialyzer is the weakest part of the ecosystem for me, and I've had `nil` and other random things sneak into my functions before despite being against the type spec. I'd much prefer to have the function call crash early by just not matching any clauses.
- mattferderer 8y agoI would love to use Elixir day to day. As the author points out, it's an enjoyable language. I highly recommend every developer to try it out, especially if you've never done any functional programming before. It has one of the best communities around.
- patrickdavey 8y agoYip, I'd love to program professionally in it someday. No idea how to get there though. I live in a smallish town in New Zealand and there are two Ruby shops here, no one is doing elixir. I can't imagine getting a remote elixir job without prod experience first. Also, as Beam, OTP etc is quite different, I'd really like to work on that with a team who really know it. Still, someday in the future, it's a great language.
- mattferderer 8y agoElixir & Ruby both seem to be two of the best languages to work in for finding remote jobs. Elixir is new enough to where I think you could show some open source code & get a job without prod experience in it. Especially if you have prod experience in other languages.
- patrickdavey 8y agoI made https://github.com/patrickdavey/caster https://github.com/patrickdavey/caster as a small toy app and I did Advent of code in elixir a couple of years ago. I haven't used it in a while though, it's just hard justifying the time at the moment. Maybe when my current (remote Ruby) job comes to an end I'll take a look. I fell into my current role, not quite sure where to start finding a remote roll! Anyway, thanks for the reply and encouragement :)
- nobleach 8y agoI sure do love Elixir, the language, the dev experience (code as documentation!!!), the docs themselves are something of a gold standard to me. (If you've ever tried to wade through OCaml docs... you feel me) The only gripe I've had is that when speed is a requirement, I have to reach for other stacks. Erlang is an uptime workhorse, it's just pretty slow compared to the JVM. Speed isn't always the most important thing... I get that. But when it is, Elixir/Erlang has some room to grow. I totally agree though, I wish I could write it every day.
- thegabez 8y agoI've always wondered what Kubernetes would be like if it was written in Erlang/Elixir instead of Go.
- danellis 8y agoUnless you're one of its developers, probably exactly the same.
- thegabez 8y agoFor example would nodes have more resiliency?
- danellis 8y agoWhat makes you say that?
- whydoineedthis 8y agoProbably not.
- mistinthewind 8y agoWe use Elixir very heavily here at PagerDuty. The vast majority of our services are written in Elixir. There's generally a shared sentiment across engineering that developing in Elixir is indeed rather satisfying. From a technical standpoint, it's highly suited to our business due to how well it does with concurrency along with its fault tolerance. Here's a nice blog post by one of our Principal engineers on Elixir, it's a great read: https://www.pagerduty.com/blog/elixir-at-pagerduty/ https://www.pagerduty.com/blog/elixir-at-pagerduty/
- neya 8y agoI'm a hardcore Ruby fan who moved to Elixir and rarely writes/maintains Ruby code. The language grows on you and once you master it, there's nothing quite like it. You could argue this for other languages - But, I am an experienced programmer who has written production applications in quite a handful of major languages and frameworks out there. I've tried almost everything that trended on HN in the past years - Scala, NodeJS, Ruby, Python, GoLang, R, PHP, Java, and a handful more and I can tell you this - there's nothing quite like Elixir for me till date. These days, even for my consulting work, I use Elixir. It's almost as if I just push code and forget. (Except for watching out for security vulnerabilities, other regular audits). Everything isn't as green since it's a fairly new language, there's still libraries lacking for some basic stuff - such as authentication (only a handful of them are maintained). But still, I wouldn't consider downgrading to anything else. Elixir has drastically improved my productivity, and you get to find errors in compile time, leading to much, much better quality, reliable code. I love how fast I can develop apps, SPAs and what not with Elixir/Phoenix. Coming from a NodeJS experience, Elixir is a 100-fold upgrade from JS. In fact, you will realize how terrible JS is (as a language) once you start programming in Elixir. The closest to Elixir that I like is Scala. They are almost like cousins. Elixir runs on Erlang's VM, Scala runs on JVM. In fact, I prefer Scala.js for my frontend these days, which converts Scala code to JS with strong typing support. But, Scala is more verbose and way too advanced for someone new to the language. There's a 101 ways to do the same thing and it's very academic as well (The Scala book was over 700 pages when I bought it). If you are looking for a new language/framework to learn in 2019 - pick Elixir + Phoenix. If not, then go with Ruby + Rails, you couldn't be wrong with either choice.
- lawn 8y ago> Elixir has drastically improved my productivity, and you get to find errors in compile time, leading to much, much better quality, reliable code. Interesting, I haven't seen that as a strong point for Elixir. Granted I'm comparing it to Haskell and Rust which really give you errors in compile time. They aren't very popular for web dev though and compared to the other languages you list I guess I can see where you're coming from.
- 8y ago
- jb3689 8y agoElixir's comparisons with Ruby are almost totally superficial. It borrows only high-level concepts from Ruby (strong metaprogramming, clean syntax, concise stdlib, excellent tooling, excellent testing tools, REPL) Elixir's stdlib is designed completely differently though and the design patterns and execution models are completely different
- holtalanm 8y agoelixir remains my favorite language. i code in java/kotlin daily and while I love all the things that kotlin provides over java (including the functional support via arrow-kt), I still find myself writing elixir whenever I have a pet project at home.
- gfodor 8y agoElixir is often said to have similar syntax to ruby but this is overstating it -- Elixir chooses a similar set of tokens in its grammar to Ruby ('def', 'if', naming rules, list/array literal delimiters, atoms/symbols, map keys) but the actual higher level syntax for the most part is very different. (And obviously the semantics have nearly nothing in common as others have mentioned.) Some clear syntactic differences (that clearly show that the design decisions were driven by building a good language over erlang, that happens to tokenize similarly to ruby, not a ruby-looking erlang powered language): - ruby uses sigils on variables to confer metadata, no similar syntax in Elixir on bindings - access modifiers are different (private vs defp) - map syntax is different (%{} vs {}), tuple syntax doesn't exist in ruby. (could have been swapped if ruby-syntax was a higher goal vs idiomatic erlang being the driving motive) - function definition syntax is slightly different, but arguably more consistent in Elixir (no do in ruby) - most of the control flow operations in ruby do not exist as macros in elixir, or have wildly different syntax and vice versa. the only exception i can think of is the humble "if" statement, which is generally avoided in elixir if it can be anyhow - closures and anonymous function syntax greatly differ - the entire syntax of pattern matching and deconstruction in elixir does not exist in ruby - no ternary operator in elixir - pipelines are central to elixir, no syntax in ruby I think Elixir's successful adoption by ruby programmers despite having nearly nothing in common with ruby than a similar choice in tokens for parsing (and even then, only when erlang doesn't pull in a different direction) show that just choosing similar tokens to a language can be a good strategy to enticing developers from that language without actually having to make any impactful design decisions (in either syntax or semantics) to favor that goal over others.
- Maultasche 8y agoI'm learning Elixir right now, and I'm enjoying it so far. I've never learned Ruby, having worked with Javascript, C#, C++, and Python in the past. Interestingly, what prepared me to understand Elixir the best were the Rambda and Bacon.js libraries in Javascript, which use a lot of functional programming concepts. I've been learning Elixir over the past couple months and writing about it on my "Learn with Me: Elixir" series at https://inquisitivedeveloper.com https://inquisitivedeveloper.com. The idea was to give any interested developers a way to learn Elixir as I learn it. I do make the occasional comparison to languages like Javascript and C#, but any experienced developer should be able to follow along. I'm getting toward the end of the basics of Elixir and starting to learn about the more intermediate functionality. I'm looking forward to getting into the more advanced features of Elixir and creating real software with it. I find it interesting that Elixir is a common upgrade path for Ruby developers. I wonder if that's actually a handicap because the syntax similarities would cause them to make the wrong assumptions about how Elixir works. The syntax is completely new to me, so I had no preconceived notions of what the syntax meant.
- PopeDotNinja 8y ago> I wonder if that's actually a handicap because the syntax similarities would cause them to make the wrong assumptions about how Elixir works. In my experience, yup. Most Ruby devs, my self included before playing with Elixir, are not accustomed to dealing with concurrency. They try to make everything synchronous, and it goes downhill from there.
- baby 8y agoVery nice article! I gotta admit I felt/feel the same way, I really like Erlang and although I also like Ruby, Elixir felt/feels weird to me and I don't see why I wouldn't write Erlang instead. I think this post has managed to convinced me to try what's on the other side of the fence for a bit.
- gcb0 8y agoI always assumed most people arriving at Elixir were Erlang neophytes, and as such, had never seen Ruby. Maybe the ruby crowd is more pronounced in the elixir IRC inner circles?
- qaq 8y ago? Most of the adoption I've seen for Elixir is Ruby shops (and I am not a Ruby developer).
- Exuma 8y agoI completely love elixir