KristerV
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?
Trending in Discussions
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
Hi there! :wave:
@frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
I’ve been using Emacs as my main code editor for more than a two years. It’s a custom build version although I’ve tried doom emacs and sp...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
New
Lately I’ve been thinking about how to organize components as a LiveView application grows. One of the pain points I’ve found (for myself...
New
What IDE or editor are you using for Elixir development?
Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
New
Other Trending Topics
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
In 2021 I started a new library called Tempo with the objective of modelling time as a set of intervals - not as instants. In 2022 I gave...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming










Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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.
KristerV
thanks for the answer, but i think you’re missing my point a bit. i’m not trying to create bug free code (which i am totally looking into atm so thanks for suggestions on those, just not what this topic is about), i’m trying to increase readability and reduce memory load.
huh? as far as i can see i just need to rewrite
MyStruct my_var =to%MyStruct{} = my_var =, shouldn’t be too hard to reorder some keywords, or what am i missing? yeah i know nothing of macros yet, not sure what you mean by context.i can read specs easily, that’s not the issue. it takes time, brainpower and memory. probably more for me than others since i have an exceptionally bad memory. i forget the type right after i close the file i was reading it from.
i’ve actually worked with typed languages very little and i’m not a fan. however there are some benefits. like less mental load (in certain situations). this is what i’m trying to achieve.
again, not the topic, but haven’t heard of gradualizer and eqwalizer. i’ll check these out.
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 = 1doesn’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.lucaong
As far as I can see, as @LostKobrakai said, this cannot be achieved with macros, because it is not valid Elixir syntax. You would have to build a transpiler, but that’s a totally different kind of problem.
I’d suggest using one of the typing solutions proposed. The issue is mostly about familiarity, and it will soon become second nature if you use it a lot.
My personal view is that a syntax like
MyStruct my_var = Module.some_function()would lead people to (erroneously) assume that this is a static type check, while%MyStruct{} = ...makes it clear this is a pattern match, which usually is amongst the first concepts learned when approaching Elixir. In fact, in many cases, when needing to access fields in the struct, one would directly do%MyStruct{ my_field: x } = ....In other words, there is a trade off between ergonomics and consistency/parsimony of the syntax. Some alternative way to express the same construct more concisely might save some keystrokes, but it adds to the cognitive load of having to understand different ways to express the same thing.
gregvaughn
You might find the built in jinterface package could help meet your needs
KristerV
oh. okay. i guess i misunderstood the whole thing.
it’s just a hassle to write every time. i don’t find autocomplete very helpful. it jumps in sometimes only.
if we were discussing changing the elixir syntac, then yeah, i’d agree. but since we’re talking package, then it’s not a problem. like i like using shorter_maps. doesn’t lead to any problems.
not sure what this is.
all in all i guess i’m going to try out the explicit syntax and see where i end up. atm i feel like it’s noisy and i may end up avoiding it and thus not having a solution at all.
aglassman
I think you will see over time that there are very few scenarios where this is syntax you would really write. In most cases, you will be invoking these functions, and evaluating them as part of a
withorcaseclause. The%MyStruct{} = my_var = foo()pattern is not necessary. This pattern match will raise a MatchError only if the result of foo() is not aMyStructstruct.In most real use scenarios, you’re going to want to do some more detailed matching, and probably have an execution path in case that match fails. So more realistic scenarios will look more like this:
or
I agree though that IDEs can be very flaky about actually showing the correct spec / docs in editor. I have many issues with this in IntelliJ, so typically just do a split panel setup with references on the on one half. I miss the robustness of JVM docs / autocomplete.
KristerV
in business logic you’re right. but in views i often just query the db and go with whatever there is and if something goes wrong an exception is a valid response.
in any case i happened on an AST tutorial and now I understand why this syntax doesn’t make sense. and also since the gradual typing research is actually moving then in the long run i think it’s okay to wait for whatever that is and then think about this whole thing again.
so for now
%ThisSyntax{} = will_have_to_do =.thanks for thinking with me everyone!