What is the alternative to the erlang -define syntax

Hi Everyone,

I know we can bind variable using
@ at the global level
I am trying to bind this example but in elixir how ?
-define(INT, 32/big-signed-integer).
I tried
@int 32-big-signed-integer,
but it doesn’t work, anyone can help me please

Thanks

You can’t do this as an attribute, as the attribute would try to evaluate at compiletime to 32 divided by the value of big-signed-integer. which is probably not defined.

So with a bit of luck you might be able to write a macro that works for you, something like

defmacro i32() do
  quote do: 32/big-signed-integer
end
2 Likes

Hey @NobbZ

I believe the marco is always the best tweak

Thank you very much

@NobbZ

how we can create a marco to simulate the “<=” BitstringPattern <= BitStringExpr.
in elixir ?

http://erlang.org/doc/reference_manual/expressions.html

This is good when doing a pattern matching on long bitstring, for example
[ {key, value} || <<key::size, value::size>> <= <<map_buffer>>]
this is kinda a mix between For loop and bitstring patternmatching.
this could be done in erlang, but in elixir I had to implement a very long logic to do so
any idea how ?

Thanks

1 Like

Perhaps this is what you are looking for?

for <<key::size, value::size <- map_buffer>>, do: {key, value}

See bitstring generators under:

https://elixir-lang.org/getting-started/comprehensions.html

3 Likes

Wow @cmkarlsson,
This is exactly what I am looking for,
you just saved my day
going to implement it now.
Thank you very much.

2 Likes