Renderingen-US

Experimental through version 1. Version 0 was alpha. This is the first language release: the interface is relatively stable, not frozen. Nothing is promised stable until version 2.

private

Translation status: English reader-locale proof. Term names and code fences follow the en pack; supporting prose may still be English.

Marks a declaration as module-private (the default tier).

Aliases: private

Syntax: @ private <declaration>

Category#

class

Examples#

radix/corpus/reference/keyword/privata/privata.fab (canonical · keyword)#

Marks a declaration as module-private (the default tier).

# =============================================================================
# private — Marks a declaration as module-private (the default tier).
# =============================================================================
#
# What this teaches:
#   • Module-level visibility — `@ private` (en spelling `@ private`) restricts
#     a declaration to the current module, keeping it out of the importable
#     surface
#   • Used as an annotation (`@ private`) on function declarations to control
#     the module boundary; unmarked declarations are module-private by default
#
# Common mistakes:
#   • Expecting `@ private` to mean anything beyond module-private — it is an
#     explicit marker for the default tier (accepted, no warning).
#
# See also: public, import
# =============================================================================

# @ private marks a declaration as private metadata
#
# Annotations attach visibility metadata to the following declaration.
#
# GRAMMAR:
#   annotatedDecl :← '@' 'private' funcDecl
#
# EXPECTED OUTPUT:
#   private

@ private
fn privatum() → string {
    return "privata"
}

main {
    print privatum()
}

Expected output:

privata

radix/corpus/reference/reject/privata/call-reject.fab (supporting · reject)#

Calling a @ private method from outside the type's own methods is a check error.

# =============================================================================
# private — private method call from outside the type
#
# A `@ private` method is callable only from the type's own methods.
# Outside the type the call is a check error: route callers through a
# public method instead.
#
# Common mistakes:
#   • Calling a private method from outside the type (`v.secret()`).
#
# See also: private, class
# =============================================================================

class V {
    mut int a
    @ private
    fn secret() → int { return 1 }
    fn open() → int { return self.secret() }
}

main {
    let V v ← V { a = 1 }
    print v.open()
    print v.secret()
}

Expected: compilation rejects this example.

radix/corpus/reference/reject/privata/read-reject.fab (supporting · reject)#

Reading a @ private field from outside the type's own methods is a check error.

# =============================================================================
# private — private field read from outside the type
#
# A `@ private` field is reachable only from the type's own methods.
# Outside the type it is not part of the surface: expose a method that
# returns what callers need instead of reaching into the field.
#
# Common mistakes:
#   • Reading a private field from outside the type (`v.a`).
#
# See also: private, class
# =============================================================================

class V {
    @ private
    mut int a
    fn get() → int { return self.a }
}

main {
    let V v ← V { a = 1 }
    print v.get()
    print v.a
}

Expected: compilation rejects this example.

radix/corpus/reference/reject/privata/write-reject.fab (supporting · reject)#

Storing to a @ private field from outside the type's own methods is a check error.

# =============================================================================
# private — private field store from outside the type
#
# A `@ private` field is writable only from the type's own methods.
# Outside the type a store is a check error even when the binding is
# `mut`: an exported method is the way to change private state.
#
# Common mistakes:
#   • Storing to a private field from outside the type (`v.a ← 4`).
#
# See also: private, class
# =============================================================================

class V {
    @ private
    mut int a
    fn set(int x) → void { self.a ← x }
    fn get() → int { return self.a }
}

main {
    mut V v ← V { a = 1 }
    v.a ← 4
    print v.get()
}

Expected: compilation rejects this example.