class
Translation status: English reader-locale proof. Term names and code fences follow the en pack; supporting prose may still be English.
Declares a concrete type with fields and methods.
Aliases: class, struct
Syntax: class <name> [<type-params>] <block>
Category#
class
Related#
Examples#
radix/corpus/reference/keyword/genus/genus.fab (canonical · keyword)#
Declares a concrete type with fields and methods.
# =============================================================================
# class — Declares a concrete type with fields and methods
# =============================================================================
#
# What this teaches:
# • Declares a concrete type with fields and methods.
# • Related keywords: interface, implements
#
# Common mistakes:
# • Using retired bare object literals (`{ key = expr }`) — use `GenusName { field = expr }` instead; bare `{ }` is now a JSON value literal.
#
# See also: interface, implements
# =============================================================================
# Basic class (record type) with fields
#
# class <Name> {
# <type> <property>
# <type> <property> = <default>
# }
#
# GRAMMAR:
# genusDecl :← 'class' ident '{' fieldDecl* '}'
# instance :← ident '{' fieldInit (',' fieldInit)* '}'
#
# EXPECTED OUTPUT:
# Field values for Punctum and Persona instances (defaults and overrides).
class Punctum {
mut int x
mut int y
}
class Persona {
mut string nomen
# default when omitted at instantiation
mut int aetas = 0
mut bool activus = true
}
main {
# Instantiate with all required fields
let Punctum p ← Punctum { x = 10, y = 20 }
print p.x
print p.y
# Instantiate with required + optional defaults
let Persona marcus ← Persona { nomen = "Marcus" }
print marcus.nomen
# 0 from default
print marcus.aetas
# true from default
print marcus.activus
# Override defaults explicitly
let Persona julia ← Persona { nomen = "Julia", aetas = 25, activus = false }
print julia.nomen
print julia.aetas
print julia.activus
}Expected output:
10
20
Marcus
0
true
Julia
25
false
radix/corpus/reference/reject/genus/fixum-field-assign-reject.fab (supporting · reject)#
A let class field is set only in a construction literal.
class Punctum {
let int x
mut int y
}
main {
mut Punctum p ← Punctum { x = 1, y = 2 }
p.x ← 5
}Expected: compilation rejects this example.
radix/corpus/reference/reject/genus/initializer-type-reject.fab (supporting · reject)#
A field initializer whose type does not match the declared field is a check error.
# =============================================================================
# class — a field initializer has the wrong type for the declared field
# =============================================================================
# Each field initializer in a construction literal must match the type the
# class declares for that field. A `text` initializer for an `int` field is a
# check error, so a mismatched value never enters a class.
class A { mut int x }
main {
let A a ← A { x = "text" }
print a.x
}Expected: compilation rejects this example.
radix/corpus/reference/reject/genus/missing-field-reject.fab (supporting · reject)#
Omitting a required field from a construction literal without a source is a check error.
# =============================================================================
# class — a construction literal without a source omits a required field
# =============================================================================
# A construction literal builds a complete value. Without a `from` source to
# copy from, every declared field must be named; leaving one out is a check
# error, so a class value never exists half-built.
class A {
mut int x
mut int y
}
main {
let A a ← A { x = 1 }
print a.x
}Expected: compilation rejects this example.
radix/corpus/reference/reject/genus/unknown-field-reject.fab (supporting · reject)#
A construction literal naming a field the class does not declare is a check error.
# =============================================================================
# class — a construction literal names a field the class does not declare
# =============================================================================
# Every key in a construction literal must be a declared field. A key that the
# class does not declare — a typo, or a field from another type — is a check
# error, so the value can never diverge from the declaration.
class A { mut int x }
main {
let A a ← A { x = 1, z = 2 }
print a.x
}Expected: compilation rejects this example.