Skip to content

Composition and inheritance

Composition constructs a definition using contributions from other definitions. Place the contributor names in parentheses after the new definition's name. The resulting definition has its own identity and can add or override information.

Shared product information

Composition.nxscript
Catalog Shop {
    Product Standard {
        Currency: USD;
        PriceCents: 100;
        Caption: @Title + " / " + @Currency;
        Title: "Standard product";
    }
    Product Notebook (Standard) {
        Title: "Pocket notebook";
        PriceCents: 450;
    }
}

Notebook inherits Currency, PriceCents, Caption, and Title from Standard. Its local Title and PriceCents replace the inherited scalar values.

Effective Notebook property Value
Currency USD
PriceCents 450
Title Pocket notebook
Caption Pocket notebook / USD

Caption demonstrates an important relationship: the inherited @Title and @Currency resolve in Notebook's effective definition. Caption does not remain frozen to Standard's original title. Standard itself is unchanged.

This allows a reusable definition to describe how its values relate, rather than only supplying a set of literal defaults.

Several contributors

Contributors.nxscript
Catalog Shop {
    Product Standard { Currency: USD; PriceCents: 100; }
    Product Premium { PriceCents: 600; }
    Product Notebook (Standard, Premium) { Title: "Pocket notebook"; }
    Product Pencil (Standard, Premium) { PriceCents: 90; }
}

Contributors apply left to right. For a scalar collision, the later contributor wins; the local body applies last. Notebook costs 600, Pencil costs 90, and both inherit USD. Reversing the contributors changes Notebook's price to 100.

“Inheritance” describes this reuse of contributors. It does not imply a class hierarchy, virtual methods, or an object constructor. The result is a definition in the completed model.

Arrays merge by entry identity

ArrayComposition.nxscript
Catalog Shop {
    Product Standard {
        Tags: [stock, Channel: retail];
    }
    Product Notebook (Standard) {
        Tags: [Channel: online, gift];
    }
}

Notebook's effective Tags are stock, Channel: online, gift, in that order. The named entry Channel is replaced in its existing position. The new unnamed entry gift appends. Unnamed entries do not match each other by their text.

The same rule applies to named inline-definition entries and references whose entry names come from their target definitions. A replacement entry replaces that entry as a value; it does not recursively merge two inline definitions.

An empty array adds no entries, so it does not clear an inherited array. When an array and a non-array contribute to the same property, the higher precedence value replaces the whole property. Whole-array references also participate according to their effective array values.

Child definitions are replaced as units

ChildComposition.nxscript
Catalog Shop {
    Product Standard {
        Packaging Wrap { Color: kraft; Material: paper; }
    }
    Product Notebook (Standard) {
        Packaging Wrap { Color: blue; }
    }
    Product Gift (Standard) {
        Packaging Wrap (Shop.Standard.Wrap) { Color: red; }
    }
}

Notebook's Wrap contains only Color. Its local child replaced the inherited Wrap; it did not silently retain Material. Gift explicitly composes its Wrap from Standard's Wrap, so Gift retains Material and overrides Color.

A property and a child with the same effective name are ambiguous and fail. Composition does not turn one kind of member into the other.

Compose or refer?

Use composition when you want a new definition assembled from contributors. Use a reference when you want to identify an existing definition. Notebook composed from Standard and a Featured property referring to Notebook express two different relationships.

Contributor definitions are ordinary definitions. They are not automatically classified as abstract or hidden from output. Modules let you keep reusable bases available without contributing them as independent output.

Continue with Targets.