How to make user defined function descriptions (“docstrings”) available to julia REPL?

后端 未结 2 1772
执念已碎
执念已碎 2021-01-30 12:01

How can user defined functions (say f) have meaningful printouts when inspected via the REPL using ?for help(f)

For example imagin

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-30 12:55

    You can use the @doc macro in Julia versions 0.4 (Oct. 2015) and above.

    % julia
                   _
       _       _ _(_)_     |  A fresh approach to technical computing
      (_)     | (_) (_)    |  Documentation: http://docs.julialang.org
       _ _   _| |_  __ _   |  Type "?help" for help.
      | | | | | | |/ _` |  |
      | | |_| | | | (_| |  |  Version 0.4.0 (2015-10-08 06:20 UTC)
     _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org/ release
    |__/                   |  x86_64-apple-darwin13.4.0
    
    julia> @doc """
           Compute 2 times x minus y squared.
           """ ->
           function f(x::Float64, y::Float64)
               return 2x - y^2
           end
    f (generic function with 1 method)
    
    julia> @doc f
      Compute 2 times x minus y squared.
    

    Edit: As pointed out by @Harrison Grodin, versions 0.5 and above support an abbreviated syntax as well as Markdown, LaTEX, and a few other goodies:

    """
    Calculate the left Riemann sum[^1] approximating ``\int_a^b f(x) dx = F(b) - F(a).``
    
    [^1]: Thomas G., Finney R. (1996), Calculus and Analytic Geometry, Addison Wesley, ISBN 0-201-53174-7
    """
    function rs(a, b, d, f)
    end
    

    There are more details in the documentation.

提交回复
热议问题