8000 Libraries · brombres/Rogue Wiki · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Brom Bresenham edited this page Mar 19, 2025 · 1 revision

library

Syntax

library [attributes]
library ModuleName [attributes]
library ModuleName::SubModule [attributes]

Description

A Rogue library is analogous to a C++ namespace or a Java package. It conceptually groups related clusters of functionality and it reduces the potential for name conflicts between classes and routines of different libraries.

Every class definition, routine definition, and group of "global" statements belong to one library or another. If no libraries are specified then all program elements belong to the default Rogue library.

The library context can be switched at any time by writing e.g. library Alpha. This would cause successive program elements to be organized under the Alpha library. From the context of any other library, an Alpha element named x would be accessed by writing Alpha::x. The uses command, described below, can simplify library usage.

Writing library with no name switches back to the default library.

Example

println Alpha::name  # alpha
println Beta::name   # beta

library Alpha
routine name->String
  return "alpha"
endRoutine

library Beta
routine name->String
  return "beta"
endRoutine

Library File Organization

If you're defining a library called Alpha, the recommended approach is to have a folder called Alpha containing a file called Alpha.rogue, which in turn includes library-related files with $include Alpha/XZY etc. For example, your source folder might contain:

Alpha/
  Alpha.rogue
  X.rogue
  Y.rogue

Alpha.rogue would contain:

$include Alpha/X
$include Alpha/Y

Attributes

Attribute Description
api Every class in the library is marked [api], which declares the class and all methods to be [essential] (immune to culling for being unused).
essential Every class in the library is marked [essential], which keeps the class from being culled for being unused.

uses

Syntax

uses ModuleName
uses Path/ModuleName
uses Path/ModuleName [export | noInclude]
uses Path/ModuleName::SubModule

A uses Path/ModuleName directive causes two things to happen:

  1. An $include Path/ModuleName directive is implicitly issued. Any :: submodule separators are converted to / for the purposes of the $include.
  2. The prefix ModuleName is added to the current library's list of used libraries that allows ModuleName classes and routines to be found without having to write the ModuleName:: prefix.

The [export] attribute means that when another library uses the current library, it also implicitly uses any exported libraries that the current library uses.

The [noInclude] attribute prevents Rogue from $includeing any files when a uses command is issued.

If multiple used libraries contain the same element name, writing the unqualified ambiguous name (without a ModuleName:: prefix) will match the current library first, then all used libraries in order of their declaration.

Example

library Alpha

routine name->String
  return alpha
endRoutine

routine alpha->String
  return "alpha"
endRoutine

---------------------

library Beta
uses Gamma [export]
uses Delta

routine name->String
  return beta
endRoutine

routine beta->String
  return "beta"
endRoutine

---------------------

library Gamma
routine gamma->String
  return "gamma"
endRoutine

---------------------

library Delta
routine delta->String
  return "delta"
endRoutine

---------------------

library  # back to the default library
uses Alpha
uses Beta
println alpha  # alpha (no need to write Alpha::alpha)
println beta   # beta
println gamma  # gamma (uses Beta and Beta exports its 'uses Gamma')
println delta  # ERROR! (can't find 'delta' in any used namespaces)
println Delta::delta  # delta (there we go)
println name          # alpha (Alpha::name() and Beta::name() both exist, but Alpha was listed first)
println Alpha::name   # (Can still be explicit)
println Beta::name    # beta
Clone this wiki locally
0