Search code examples
garbage-collectionerlang

Are Erlang funs garbage collected?


If one generates funs in erlang, are they garbage collected? For instance to replicate a python like iterator or to read a stream, one may generate funs for every yield -- potentially a great many. The used funs are no longer required after use, so are they eventually garbage collected?

I guess that is to say, is such an object as #Fun<erl_eval.xx.yyyyyyy> an atom?


Solution

  • I guess that is to say, is such an object as #Fun<erl_eval.xx.yyyyyyy> an atom?

    No, a fun is not an atom, rather a fun is its own data type. At some point, you will see the following ordering of erlang's data types in the docs:

    number < atom < reference < fun < port < pid < tuple < map < nil < list < bit string
    

    which means you can write things like this:

    1> 12 < fun(X) -> X end. 
    true
    
    2> abc < fun(X) -> X end.
    true
    
    3> [1, 2, 3] < fun(X) -> X end.
    false