3 ms·
I've been writing Elixir professionally for a couple months now and I find it fascinating how many tools Elixir gives you to play with. However, I started a to
by Kaze404 6y ago
I've been writing Elixir professionally for a couple months now and I find it fascinating how many tools Elixir gives you to play with.
However, I started a toy project in it and immediately ran into trouble due to its dynamic nature. Printing function arguments to see what they are, runtime errors... Is there a way to get around this besides adding spec tags on everything?
- jswny 6y agoIt depends on what you are struggling with. If you want formal type documentation and some decent type checking, Dialyzer is pretty good. That’s really the only solution for Elixir. If you want full static type checking, there is https://gleam.run/ https://gleam.run/ which is a new language, and there’s also something that seems like a full static type checker for Erlang which is going to be released by Facebook soon (they use it internally). See this: https://elixirforum.com/t/facebook-is-writing-a-new-statically-typed-language-to-run-on-the-beam/29829 https://elixirforum.com/t/facebook-is-writing-a-new-statical.... I, for one, am hoping that the Facebook solution allows us to implement something in Elixir to interface with it so that we can take advantage of it in Elixir, but that is yet to be seen.
- Kaze404 6y agoI've been using Dialyzer and it works well enough most of the time. Maybe the Elm compiler spoiled me, but when Dialyzer tells me "this pattern will never match" and not offer me tools to fix it, it's very frustrating. I'm not sure if it's possible, but it would be nice to have an action to autocomplete missing patterns like in Elm / Rust. That said, I'll take a look at Gleam. Thank you
- jswny 6y agoYeah the big problems with Dialyzer are that it can be slow at times and that the error messages are not great. However, a lot of the error messages are the same esoteric messages, and you can get used to debugging them when you know what they are trying to call out.
- elcritch 6y agoDialyzer messages can be pretty obtuse. Usually I take it as a cue to just re-read the function and it's often somewhat clear where the problem lies due to immutability (not always though). Another trick I'll do, is to use (abuse) pattern matching: def foo(%SomeStruct{} = arg, value) when is_integer(value) ... That also helps Dialyzer narrow down possibilities too and will blow up on runtime. If you use VSCode, the ElixirLS module will auto-suggest @spec's for your functions.
- lpil 6y agoHello! I'm the creator of Gleam. From talking to the WhatsApp Erlang team a little it seems unlikely unlikely that the Erlang type checker will be usable with Elixir I'm afraid.
- romanoderoma 6y ago> Is there a way to get around this besides adding spec tags on everything? things are moving in that space, at least for Erlang https://codesync.global/uploads/media/activity_slides/0001/03/f2292f201aa6b04db8c4e0b9cfa191dd07c9ee14.pdf https://codesync.global/uploads/media/activity_slides/0001/0...
- pera 6y agoAFAIK you don't need to add @specs everywhere to get useful warnings from Dialyzer (or Dialyxir) as it does some type inference and also the standard library is already annotated, but I only played with it one time so don't quote me on that.
- triplejjj 6y agoFun fact: dialyzer does not use your @spec’s to run checks. If you do provide a @spec, it will check that it matches, but it doesn’t affect dialyzer itself.
- ch4s3 6y agoYou may get some mileage out of reading up on structural typing, and thinking about how you can apply that to your function heads. You can write several versions of functions that are explicit about the shape and type of data they expect. If you're thoughtful about it, then you'll run into fewer errors.