t27.aiРусский

pub or private

You will learn

What pub declares, and what the TypeScript backend writes with and without it.

pub in front of const, fn, struct or enum says other modules may use the name. Without pub the name is meant for the module itself. Today the compiler does not enforce this in the code it writes: gen-ts exports both the pub and the private constant of the lesson spec. Use pub to say what a module offers, and read the generated code before relying on privacy.

Try it

Open the ts tab of the lesson spec in the player and find both constants.

Open the interactive lesson →

t27 basics 16: what pub changes in the generated code
t27 basics 16: what pub changes in the generated code ↗

A pub and a private t27 constant side by side with the TypeScript the real compiler wrote for each. Lesson 16 of the t27 basics course.

specs/basics/16_pub_or_private.t27

// SPDX-License-Identifier: Apache-2.0
; t27 basics, lesson 16: pub or private.
; pub marks a name for other modules; without it the name is the module's own.

module basics_16_pub_or_private;

pub const OFFERED : u8 = 1;
const OWN : u8 = 2;

test both_names_exist_in_the_module {
    assert OFFERED == 1;
    assert OWN == 2;
}

Open the lesson's spec in the player ↗

All lessons