Skip to content

References

A reference connects a value to something already declared. Prefix its path with @. The target can be a property, a definition, or a named array entry.

References.nxscript
Catalog Shop {
    Product Notebook { Title: "Pocket notebook"; PriceCents: 450; }
    Product Pencil { Title: "Graphite pencil"; PriceCents: 90; }
    Featured: @Notebook;
    FeaturedTitle: @Featured.Title;
    Captions: [Short: "Shop", Long: "Corner Stationery"];
    Heading: @Captions.Long;
    Offers: [@Notebook, @Pencil];
    SameOffers: @Offers;
}

Compile this file as in the first chapter. Featured identifies the existing Notebook definition. FeaturedTitle follows that relationship and obtains Pocket notebook. Heading resolves to the named scalar entry's text. SameOffers refers to the whole Offers array.

This is useful because the featured-product relationship remains attached to Notebook when its title or price changes. You do not need to maintain a second product description.

In the native model, references to the same definition retain the same target. JSON must represent that relationship as data; its projection is explained in Artifacts. Do not infer copying semantics from the appearance of serialized JSON.

Scope and qualified paths

Within a definition, a single-segment reference such as @Title looks in that definition's members. It does not search every ancestor for a property with that name. An explicitly imported root can also be referenced by its name.

Use a qualified path to cross an enclosing scope:

Scope.nxscript
Catalog Shop {
    Currency: USD;
    Product Notebook {
        Title: "Pocket notebook";
        Label: @Title + " / " + @Shop.Currency;
    }
}

Inside Notebook, @Title means Notebook's Title. @Shop.Currency explicitly names its enclosing catalog. Replacing it with @Currency would be unresolved.

A qualified path can start with an accessible member, an enclosing definition's name, or an imported root. It can traverse child definitions, definition-valued properties, and named array entries. There is no numeric array-index syntax in reference paths; name an entry when you need to address it.

Separate roots in one file do not become a universal namespace for one another. Use an enclosing definition for related members or import shared roots from a module. A qualified path documents which relationship you intend to follow. The exact lookup sequence is in the reference.

References and declaration order

Forward.nxscript
Catalog Shop {
    Heading: @Title;
    Title: "Corner Stationery";
}

Heading resolves even though Title appears later. These are declarations, not sequential assignments. A consumer may use declaration order for its own work, but that does not turn the language's references into execution statements.

Cycles have different meanings

Two concrete definitions may refer to each other: a product can refer to its category while the category refers to its featured product. The Live graph retains such links. A serialization format may impose additional constraints.

A scalar dependency such as A: @B; B: @A; cannot produce a value and fails compilation. A composition cycle likewise cannot construct a completed definition. These are different failures from a valid object relationship that a particular emitter cannot expand.

Continue with Composition and inheritance.