RobPazzuzu7
RobPazzuzu7

Reputation: 69

How can I create a library in julia?

I need to know how to create a library in Julia and where I must keep it in order to call it later. I come from C and matlab, it seems there is no documentation about pratical programming in Julia. Thanks

Upvotes: 1

Views: 2576

Answers (1)

Julia Learner
Julia Learner

Reputation: 2902

If you are new to Julia, you will find it helpful to realize that Julia has two mechanisms for loading code. Stating you "need to know how to create a library in Julia" would imply you most likely will want to create a Julia module docs and possibly a packagedocs. But the first method listed below may also be useful to you.

The two methods to load code in Julia are:

1. Code inclusion via the include("file_path_relative_to_call_or_pwd.jl")docs

The expression include("source.jl") causes the contents of the file source.jl to be evaluated in the global scope of the module where the include call occurs.

Regarding where the "source.jl" file is searched for:

The included path, source.jl, is interpreted relative to the file where the include call occurs. This makes it simple to relocate a subtree of source files. In the REPL, included paths are interpreted relative to the current working directory, pwd().

Including a file is an easy way to pull code from one file into another one. However, the variables, functions, etc. defined in the included file become part of the current namespace. On the other hand, a module provides its own distinct namespace.

2. Package loading via import X or using Xdocs

The import mechanism allows you to load a package—i.e. an independent, reusable collection of Julia code, wrapped in a module—and makes the resulting module available by the name X inside of the importing module.

Regarding the difference between these two methods of code loading:

Code inclusion is quite straightforward: it simply parses and evaluates a source file in the context of the caller. Package loading is built on top of code inclusion and is quite a bit more complex.

Regarding where Julia searches for module files, see docs summary:

The global variable LOAD_PATH contains the directories Julia searches for modules when calling require. It can be extended using push!:

push!(LOAD_PATH, "/Path/To/My/Module/")

Putting this statement in the file ~/.julia/config/startup.jl will extend LOAD_PATH on every Julia startup. Alternatively, the module load path can be extended by defining the environment variable JULIA_LOAD_PATH.

For one of the simplest examples of a Julia module, see Example.jl

module Example
export hello, domath

hello(who::String) = "Hello, $who"
domath(x::Number) = x + 5

end

and for the Example package, see here.

Side Note There is also a planned (future) library capability similar to what you may have used with other languages. See docs:

Library (future work): a compiled binary dependency (not written in Julia) packaged to be used by a Julia project. These are currently typically built in- place by a deps/build.jl script in a project’s source tree, but in the future we plan to make libraries first-class entities directly installed and upgraded by the package manager.

Upvotes: 4

Related Questions