KristerV
String text = ”data type in Elixir”;
i’ve recently gotten annoyed at having to jump between files just to check what that function i’m using will return. VS Code could be of help by using @spec, but frankly that too is pretty dense to read.
i would like this Elixir syntax
%MyStruct{} = my_var = Module.some_function()
to be easily readable like this
MyStruct my_var = Module.some_function()
this is just a readability thing. imagining a large function full of this kind of rows i’d much prefer tha latter syntax. it should reduce load on memory by a lot.
before i jump in to learn macros just to create this thing, i thought i’d ask you guys - is this approach a bad idea? is there something out there already? is there a better approach completely?
Most Liked
LostKobrakai
There’s two parts to this. There’s the elixir parser, which requires you to write correct elixir syntax. Otherwise it cannot parse the code. Once the code is parsed by the parser (into AST) macros can come in and change their input AST – could be code blowing up when trying to be executed – to some different output AST – which hopefully runs just fine when executed.
MyStruct my_var = isn’t valid elixir syntax – quote do: MyStruct my_var = 1 doesn’t succeed, so there’s nothing a macro could do here.
Not really sure why you’d like this to change the syntax here. If you want the structs be listed why not write %MyStruct{} = my_var = …? That just works and can even help out editor autocomplete.
gregvaughn
You might find the built in jinterface package could help meet your needs ![]()
hst337
First of all, this is pretty hard to do with macros. You’ll need to create context-aware AST traversal function to do this right.
Second, just learn to read specs, they’re not that hard to read, to be honest.
It depends on what you want to achieve. I guess that you’re saying something like “I used to work with static typing, and I’d like to have this in Elixir”, but this is generally a bad approach, since Elixir is dynamically typed language by design.
So, there are a lot of different ways to achieve what you want with dynamic typing. If you want to find errors and typos during development, you can use dialyzer which has integrations with every editor. If you want to find errors at compile-time, you can use dialyzer, gradualizer, eqwalizer, etc and you can write unit-tests of course.








