samedi 15 mai 2021

Syntax to automatically export functions in a Julia module

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:

Aucun commentaire:

Enregistrer un commentaire