0.45+9.2.0.3

8.5 Exports: export🔗ℹ

By default, all of a module’s definitions are private to the module. The export form specifies definitions to be made available where the module is imported.

export:

  export_clause

  ....

An export form can only appear in a module body or a namespace body. Specifying multiple export_clauses in a single export is the same as using multiple export forms each with a single export_clause.

Each identifier can be exported at most once from a module across all exports within the module. More precisely, the external name for each export must be distinct; the same internal binding can be exported multiple times under different external names.

The simplest export_clause is just an identifier that names a binding within the module. The binding can be from either a local definition or from an import.

export:

  color

  pi

 

def color = "blue"

def pi = 3.14

The export form also can be combined with a definition to export the names defined by the definition:

export def color = "blue"

 

export fun greet(who):

  "Hello, " ++ who

When a class or namespace is exported, bindings introduced by the class or namespace—such as the constructor, accessors, predicates, and methods, or the bindings inside a namespace—are exported along with it. So, for example,

export:

  Posn

 

class Posn(x, y)

exports Posn, Posn.x, Posn.y, and so on.

A rename form is similar to just specifying an identifier, but the exported binding orig_id is given a different name, export_id, to importing modules.

export:

  rename:

    hue as color

 

def hue = "blue"

export

all_from(module_name)

 

export

all_from(. namespace_id)

The all_from form with a module_name exports all bindings in the module that were imported and exposed from that module. This form normally makes sense with a module that is imported with open or expose.

import:

  "f2c.rhm" open

export:

  all_from("f2c.rhm")

The all_from form with .namespace_id re-exports everything that is exported by namespace_id, independent of the bindings that are imported or exposed from the namespace. A module import without open binds a namespace (based on the imported module’s name), so this form of all_from can be used to re-export all of the bindings of another module.

import:

  "f2c.rhm"

namespace extras:

  export: def

  def freezing_f = 32

export:

  all_from(.f2c)

  all_from(.extras)

export

except export_clause

 

export

except:

  export_clause

  ...

The except modifier inverts the meaning of an export_clause to exclude the names that it describes, instead of including them. The omitted names are the external names of the bindings, and except attaches to another export_clause either through the block under the clause or as a prefix.

import:

  "f2c.rhm" open

 

export:

  all_from("f2c.rhm"):

    except fahrenheit_freezing