Syntax to automatically export functions in a Julia module

Viewed 358

I find it a little tedious to list out every function for exporting. And doing this (pulled from a thread a couple of years ago) feels bad:

module DemoModule
  # 25 functions here...

  for n in names(current_module(), true)
    if Base.isidentifier(n) && n ∉ (Symbol(current_module()), :eval)
      @eval export $n
    end
  end
end

Is there syntactic sugar to mark functions to be exported automatically? I'd like to draw an analogy to Python's syntax for public/private methods:

module DemoModule

  function add(a, b)
    # `add` is exported automatically
    return _private_helper(a) + _private_helper(b)
  end

  function _private_helper(a)
    # `_private_helper` should not be exported, so it's marked with a `_`
    return a
  end
end

Some Additional Context:


Edit: Came up with a possible compromise: ExportPublic.jl

1 Answers

This is generally considered not recommended throughout discussions in Julia community.

However there is a package ExportAll.jl that just exactly that.

module Bar
    using ExportAll
    function foo()
        1
    end

    function bar()
        2
    end
    @exportAll()
end

Now you can do:

julia> using Main.Bar

julia> bar()
2
Related