How to call a fun from a NIF?

,

As far as I’m aware and I used port drivers in GRiSP extensively when statically linked NIFs were still not implemented, there is currently nothing anymore you can do with port drivers which can’t be done with NIF (better).

I did a cursory check of lists module implementation and found that all functions which take a fun as a argument are implemented in pure Erlang. That shows us two things:

  1. its not even possible for BIFs to call a fun (or not easy), otherwise I would have looked at these implementations to see how we could add the functionalities for NIFs
  2. If its fast enough for lists its probably fast enough for your application

Another look at ets:foldl/3 implementation which is similar to your intended implementation and needs a way to iterate through all elements in an ets table and call a fun on them:

You can see it uses ets:first and ets:next to iterate through the table and calls the fun from Erlang. Thats exactly how you will want to implement this.

9 Likes