KristerV

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

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

gregvaughn

You might find the built in jinterface package could help meet your needs :smirk:

hst337

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.

Last Post!

KristerV

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!

Where Next?

Popular in Discussions Top

CharlesO
Erlang :list.nth simple, but 1 - based nth(1, [H|_]) -> H; nth(N, [_|T]) when N > 1 -> nth(N - 1, T). Elixir Enum.at … coo...
New
joeerl
I’m playing with Elixir - It’s fun. I think @rvirding does give Elixir courses these days. Re: files and database - when I given Erlang ...
New
New
AstonJ
If so I (and hopefully others!) might have some tips for you :slight_smile: But first, please say which area you’re finding most challen...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
marciol
Please, let me know if this kind of discussion already took place in another topic . Hi all, how do you consider if is better to build ...
New
pillaiindu
I want to convert a Phoenix LiveView CRUD website to a CRUD mobile app. What do you think is the easiest way to do so?
New

Other popular topics Top

hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44608 311
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement