See also previous discussions, which include a link to exactly this topic in the Julia manual: Implement of Matlab function "nargout" in Julia
Two other options:
- If you just do something that changes the input argument types, you can return an additional result by dispatching to a different method. For example, you could pass
WantsFooToo()as an argument, whereWantsFooToois a singleton type, and it could dispatch to a different method that also computes and returns some data “foo” - You can return an object that computes the additional data on demand.
For an exampe, Julia’s linear-algebra factorizations often return a “factorization objection” that have overloaded getproperty to compute additional data, such as the explicit Q matrix from a QR factorization (which otherwise is stored implicitly).
Another example is reverse-mode AD systems like Zygote.jl and ChainRules.jl, where the adjoint rule is returned as a pullback function object that computes a vector–Jacobian product only if/when it is actually needed.