Julia Language Modules Wrap Code in a Module


Example

The module keyword can be used to begin a module, which allows code to be organized and namespaced. Modules can define an external interface, typically consisting of exported symbols. To support this external interface, modules can have unexported internal functions and types not intended for public use.

Some modules primarily exist to wrap a type and associated functions. Such modules, by convention, are usually named with the plural form of the type's name. For instance, if we have a module that provides a Building type, we can call such a module Buildings.

module Buildings

immutable Building
    name::String
    stories::Int
    height::Int  # in metres
end

name(b::Building) = b.name
stories(b::Building) = b.stories
height(b::Building) = b.height

function Base.show(io::IO, b::Building)
    Base.print(stories(b), "-story ", name(b), " with height ", height(b), "m")
end

export Building, name, stories, height

end

The module can then be used with the using statement:

julia> using Buildings

julia> Building("Burj Khalifa", 163, 830)
163-story Burj Khalifa with height 830m

julia> height(ans)
830