reflect.Class

A class-kind view of a `type` value. Users never construct this class.

Reference version

Signature

class reflect.Class

A class-kind view of a type value. Users never construct this class.

Source:<builtin>/reflect/kinds.bamlbytes 75–4475

Fields

_ty

reflect.Type

The wrapped type value. Private by invariant: reflection natives are its only readers, and each kind method panics if the held type no longer classifies as this view's kind.

Static methods

function

builder

(name: string) -> reflect.class.Builder throws never

Start an incrementally-defined runtime class. Builders are required when one or more fields recursively refer to the class being defined.

function

extends

<I>(
name: string,
) -> reflect.Class throws reflect.errors.CompilationError

Construct a closed runtime class that implements the interface I.

The class declares every field of I and of the interfaces I requires, transitively, followed by fields. A required interface's fields come before the fields of the interface that requires it, and required interfaces are visited in the order their requires clause lists them, so for interface FooBar requires HasA, HasB { c: int } the class declares a, b, c, then fields. An interface reached through two paths contributes its fields once, at their first position. Each interface field keeps its doc comment.

The class implements I and every interface in its requires closure, each interface field satisfied by the class field of the same name. Interface default methods apply to the class as usual.

Throws

  • reflect.errors.CompilationError if I is not an interface, if an interface in the closure has a method without a default body, if fields names a field an interface already declares, or if two interfaces declare the same field with different types.
function

get_field

<T>(value: unknown, name: string) -> T throws reflect.errors.CompilationError

Assert-read a named field from a runtime-shaped class instance.

Instance methods

function

fields

(self) -> reflect.class.Field[] throws never

The class's fields, in declaration order. Ownerless: these describe the type, so Field.value on them returns null.

function

metadata

(self) -> reflect.Meta throws never

The class declaration's own schema metadata.

function

name

(self) -> string throws never

The class's name, rendered like to_string: Runtime, shape.Inner, Box<int>.

Implementations

reflect.AnyClass for T

Instance methods

function

attributes

(self) -> reflect.Meta throws never

The class declaration's schema metadata, or empty metadata when the runtime type is not a class.

function

find

<T>(self, name: string) -> T | null throws reflect.errors.TypeMismatch

Read the field called name as T, or null if this value's class declares no such field. A present field holding null also reads as null; use has_field or get<T?> to tell the two apart.

Throws

  • reflect.errors.TypeMismatch if the field's value is not a T.
function

find_field

(self, name: string) -> reflect.class.Field | null throws never

A value-bound handle to the field called name, or null if this value's class declares no such field.

function

get

<T>(self, name: string) -> T throws reflect.errors.NotFound | reflect.errors.TypeMismatch

Read the field called name as T. Read a nullable field with a nullable T (get<int?>). find returns null for a missing field instead of throwing.

Throws

  • reflect.errors.NotFound if this value's class declares no field called name.
  • reflect.errors.TypeMismatch if the field's value is not a T.
function

get_field

(self, name: string) -> reflect.class.Field throws reflect.errors.NotFound

A value-bound handle to the field called name. find_field returns null instead of throwing.

Throws

  • reflect.errors.NotFound if this value's class declares no field called name.
function

has_field

(self, name: string) -> bool throws never

Whether this value's runtime class declares a field called name. false when the runtime type is not a class.

function

list_fields

(self) -> reflect.class.Field[] throws never

Reflected fields bound to this value for Field.value<T>().

function

name

(self) -> string throws never

The rendered name of this value's concrete runtime type.

function

type

(self) -> reflect.Type throws never

This value's concrete runtime type.

Source:<builtin>/reflect/reflect.bamlbytes 14431–14461

reflect.TypeView for reflect.Class

Instance methods

function

array

(self) -> reflect.Array throws never

Construct an array type whose element is this type.

function

implemented_by

(self, other: reflect.TypeView) -> bool throws never

Reverse of implements: iface.implemented_by(t) is the same as t.implements(iface). false when this type is not an interface, or when other is not a concrete type.

function

implements

(self, other: reflect.TypeView) -> bool throws never

True iff this type nominally implements the interface other. Any concrete type can: a class, an enum, a primitive, a list, a map, ... other pins the interface's associated types (an omitted one takes its default), and only an implementation with exactly those matches: see reflect.Type.implements. false when other is not an interface, or when this type is not a concrete type.

function

kind

(self) -> reflect.TypeKind throws never

Returns the precise kind view of this type value.

function

meta

(
self,
alias: string | null = …,
description: string | null = …,
docstring: string | null = …,
other: map<string, string> | null = …
) -> reflect.WithMeta<reflect.Type> throws never

Pair this type value with schema metadata without changing its identity.

function

optional

(self) -> reflect.Union throws never

Construct the canonical union of this type and null.

function

to_baml

(self) -> string throws reflect.errors.CompilationError

Render deterministic canonical BAML source for this definition. Non-equivalent declarations with the same display name are rejected.

Source:<builtin>/reflect/kinds.bamlbytes 3667–3837