kokolegorille

kokolegorille

Hello everyone,

I have some strange error message when using :ets.fun2ms.

I am trying to filter an ets table based on some time.

def get_obsolete_check_in(time) do
  fun = :ets.fun2ms(fn {_, %CheckIn{start_time: start_time}} = check_in when start_time > time ->
    check_in
  end)
  :ets.select(:check_ins, fun)
end

Here is the error message.

** (exit) exited in: :ets.fun2ms(:function, :called, :with, :real, :fun, :should, :be, :transformed, :with, :parse_transform, :or, :called, :with, :a, :fun, :generated, :in, :the, :shell)
    ** (EXIT) :badarg
    (stdlib) ets.erl:580: :ets.fun2ms/1
    (next_engine) lib/next_engine/check_in_server.ex:30: NextEngine.CheckInServer.get_obsolete_check_in/0

This also happens with a simple function like

fun = :ets.fun2ms(fn check_in -> check_in end)
:ets.select(:check_ins, fun)

Looking for an answer I found similar problems in Erlang here. And it seems the Erlang solution would be to include…

-include_lib("stdlib/include/ms_transform.hrl").

How does this translate to Elixir?

Thanks for taking time.

Showing Posts 1 to 10

rvirding

rvirding

Creator of Erlang

The problem is that the fun must be converted to a match spec at compile time. In Erlang the “ms_transform.hrl” file causes a parse transform to be run which at compile time recognises the call to :ets.fun2ms/1 and converts the fun to a match spec. It basically behaves like a macro. If the function is called at run-time you get the error.

How this is handled in Elixir I don’t know.

EDIT: Funnily enough I have just been working fixing the handling of match specs in LFE (Lisp Flavoured Erlang), amongst other things writing and ets-ms and dbg-ms macros which do this conversion.

kokolegorille

kokolegorille OP

Thank You for your response.

I tried this simple module, it fails with the same error. But if I type each line in the console, it’s working.

defmodule NextEngine.Koko do
  def do_work() do
    :ets.new(:checks, [:set, :protected, :named_table])
    fun = :ets.fun2ms(fn check_in -> check_in end)
    :ets.select(:checks, fun)
    :ets.delete(:checks)
  end
end

iex> NextEngine.Koko.do_work()
** (exit) exited in: :ets.fun2ms(:function, :called, :with, :real, :fun, :should, :be, :transformed, :with, :parse_transform, :or, :called, :with, :a, :fun, :generated, :in, :the, :shell)
    ** (EXIT) :badarg
    (stdlib) ets.erl:580: :ets.fun2ms/1
    lib/next_engine/koko.ex:4: NextEngine.Koko.do_work/0

I probably need to translate this line in Elixir, but I don’t know yet how to do it.

-include_lib("stdlib/include/ms_transform.hrl").
rvirding

rvirding

Creator of Erlang

This is because the shell “knows” about the :ets.fun2ms/1 function and handles it specially, and :dbg.fun2ms/1 as well.

The :dbg version is for match spec for tracing which are a little different, the :ets version is for return data while th :dbg version do tracing commands.

pmonson711

pmonson711

I’ve always used ex2ms | Hex to achieve this in Elixir.

kokolegorille

kokolegorille OP

That lib seems to be exactly what I need.

Thank You very much.

kokolegorille

kokolegorille OP

I got it working, but it is a bit more complex than previously thought.

for future reference, if You happen to search into an ets table storing Elixir structure by key (here by date). You will need Ex2ms, and use correct syntax.

Here is pseudo code, with 3 functions f, the last one is working.

  import Ex2ms
  ...
  def get_obsolete_check_in(time) do
    # f = :ets.fun2ms(fn {_, %CheckIn{start_time: start_time}} = record when start_time > ^time -> record end)
    
    # f = fun do {_, %Checkin{start_time: start_time}} = record when start_time > ^time -> record end
    
    f = fun do {{_, _, _, start_time}, _} = record when start_time > ^time -> record end
    
    :ets.select(:check_ins, f)
  end

The first try with :ets.fun2ms works in iex, but fails when defined in a module with error.

** (exit) exited in: :ets.fun2ms(:function, :called, :with, :real, :fun, :should, :be, :transformed, :with, :parse_transform, :or, :called, :with, :a, :fun, :generated, :in, :the, :shell)

The second try with Ex2ms fails with error. You cannot extract start_time directly from the Elixir structure.

** (ArgumentError) parameters to matchspec has to be a single var or tuple

The solution is to store start_time in the key, so that Ex2ms can generate a match_spec from a single var, part of the key. Do not forget the little ^ when matching.

Thank You @rvirding and @pmonson711 for your kind help.

rvirding

rvirding

Creator of Erlang

Yes, while you can put structs in an ETS table you cannot put them directly in the table as the elements in an ETS table must be tuples so the struct itself must one of the elements of the tuple.

sasajuric

sasajuric

Author of Elixir In Action

For :ets.fun2ms to work, you should add @compile {:parse_transform, :ms_transform} into your module.

kokolegorille

kokolegorille OP

Thank You for the response. This will remove the need for ex2ms.

kokolegorille

kokolegorille OP

After getting some warnings with Elixir 1.6 and Erlang 20.1

warning: @compile {:parse_transform, _} is deprecated. Elixir will no longer support Erlang-based transforms in future versions

I found this issue

https://github.com/elixir-lang/elixir/issues/5762

The better way now seems to be using ex2ms, losing some supports for better compilation.

Where Next? Top

Trending in Questions Top

axelson
One of my LiveView view’s has a very heavy javascript dependency so I want to avoid loading it for every page on the site, while still us...
New
yasoob
Hi all! I know that quite a few people use Phoenix LiveView but it is hard to get a more up-to-date list of folks/companies using it. I ...
New
neurodynamic
I have a page in my Phoenix app with some LiveView code. When you click a button, it shows the Stripe credit card modal form via a Hook. ...
New
Hellowman
Hello everyone! I am new to coding with Elixir/Erlang. And i am looking for some utilities for fuzzing testing like AFL++. Does anyone ...
New
yarrichar
I’m very new to Elixir, so sorry if this is a silly question: I am wondering what happens to the mailbox of a process when the machine i...
New
mandar1045
I load tested a plain Phoenix chat app backed by Postgres with 100 million messages in it, everything on default settings, to see what br...
New
Fl4m3Ph03n1x
Background When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error: * assembling mark...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
261 13183 112
New
mudasobwa
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
OndrejValenta
(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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
charlenopires
Hi everyone :waving_hand: I’ve been working on PhiaUI, an open-source UI component library for Phoenix LiveView, and I wanted to share i...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews