reflect.Type

A runtime value describing a BAML type.

Reference version

Signature

class reflect.Type

A runtime value describing a BAML type.

reflect.Type.of<T>() describes a type argument and reflect.Type.of_value(value) describes a value's concrete runtime type. A type value's identity is its declarations': two values naming the same declarations compare equal, and a runtime-created declaration is equal only to itself. Reflection methods are exposed directly.

Source:<builtin>/reflect/type.bamlbytes 396–6504

Static methods

function

of

<T>() -> reflect.Type throws never

Return the runtime type value for the type argument T.

Concrete arguments are materialized by one LoadType instruction.

function

of_value

(value: unknown) -> reflect.Type throws never

Return the concrete runtime type of value, never a literal type.

Values with no reconstructable BAML type produce the unknown type.

Instance methods

function

_implements

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

(internal) Internal native backing implements and implemented_by; both sides are type values.

function

_to_string_impl

(self) -> string throws never

(internal) The rendering behind baml.ToString for every type value. reflect.Type and each kind view implement baml.ToString by out-of-body implement blocks beside the interface (baml/conversions.baml): baml depends on reflect, never the reverse. Code inside reflect cannot see those implementations, so it calls this method directly.

function

_validate_renderable

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

(internal) Internal shared-render-boundary guard for BEP-066 R-1 and R-3.

function

array

(self) -> reflect.Array throws never

Construct an array type whose element is this type.

function

as_array

(self) -> reflect.Array | null throws never

The array view of this type, or null when it does not describe an array.

function

as_class

(self) -> reflect.Class | null throws never

The class view of this type, or null when it does not describe a class.

function

as_enum

(self) -> reflect.Enum | null throws never

The enum view of this type, or null when it does not describe an enum.

function

as_function

(self) -> reflect.Function | null throws never

The function view of this type, or null when it does not describe a function.

function

as_interface

(self) -> reflect.Interface | null throws never

The interface view of this type, or null when it does not describe an interface.

function

as_literal

(self) -> reflect.Literal | null throws never

The literal view of this type, or null when it does not describe a literal.

function

as_map

(self) -> reflect.Map | null throws never

The map view of this type, or null when it does not describe a map.

function

as_primitive

(self) -> reflect.Primitive | null throws never

The primitive view of this type, or null when it does not describe a primitive.

function

as_union

(self) -> reflect.Union | null throws never

The union view of this type, or null when it does not describe a union.

function

implemented_by

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

Reverse of [implements]: iface.implemented_by(t) is the same as t.implements(iface), with the same associated-type rule. Useful when the call site naturally reads "does this interface include that type?".

function

implements

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

True iff the receiver type nominally implements the interface other (BEP-044): an implements other { ... } block in the receiver's body, or an implement other for ... block anywhere in the program, applies to it with its bounds satisfied. The receiver can be any concrete type: a class, an enum, a primitive, a list, a map, ...

other is an interface type, so it names every associated type the interface declares without a default, and an omitted one takes its default: baml.iter.Iterator<Item = int> is baml.iter.Iterator<Item = int, Error = never>. Only an implementation with exactly those associated types matches, so an int iterator that throws does not implement Iterator<Item = int>, and there is no way to ask "is this any iterator".

Returns false when other is not an interface, or when the receiver is not a concrete type (an interface does not implement itself).

function

kind

(self) -> reflect.TypeKind throws never

Returns the precise kind view of this type value: a freshly allocated view instance wrapping the receiver in its _ty field.

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.

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.Type

Instance methods

function

array

(self) -> reflect.Array throws never

Construct an array type whose element is this type.

function

as_type

(self) -> reflect.Type throws never
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/type.bamlbytes 926–1044