Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// An array-kind view of a `type` value. Users never construct this class.
class Type {
implements baml.reflect.TypeView {
//baml:vm
function as_type(self) -> type throws never { $rust_function }
}
//baml:mut_vm
function element_type(self) -> type throws never { $rust_function }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Field {
name string
type type?
meta baml.reflect.Meta
}

/// A class-kind view of a `type` value. Users never construct this class.
class Type {
implements baml.reflect.TypeView {
//baml:vm
function as_type(self) -> type throws never { $rust_function }
}
//baml:mut_vm
function fields(self) -> Field[] throws never { $rust_function }
//baml:mut_vm
function meta(self) -> baml.reflect.Meta throws never { $rust_function }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Value {
name string
meta baml.reflect.Meta
}

/// An enum-kind view of a `type` value. Users never construct this class.
class Type {
implements baml.reflect.TypeView {
//baml:vm
function as_type(self) -> type throws never { $rust_function }
}
//baml:mut_vm
function values(self) -> Value[] throws never { $rust_function }
//baml:mut_vm
function meta(self) -> baml.reflect.Meta throws never { $rust_function }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Parameter {
name string?
type type
optional bool
}

/// A function-kind view of a `type` value. Throws are intentionally not part
/// of the slice-1 reflection surface.
class Type {
implements baml.reflect.TypeView {
//baml:vm
function as_type(self) -> type throws never { $rust_function }
}
//baml:mut_vm
function params(self) -> Parameter[] throws never { $rust_function }
//baml:mut_vm
function return_type(self) -> type throws never { $rust_function }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// An interface-kind view of a `type` value. Users never construct this class.
class Type {
implements baml.reflect.TypeView {
//baml:vm
function as_type(self) -> type throws never { $rust_function }
}
//baml:vm
function implemented_by(self, other: type) -> bool throws never { $rust_function }
//baml:mut_vm
function implementors(self) -> type[] throws never { $rust_function }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/// A literal-kind view of a `type` value. Users never construct this class.
class Type {
implements baml.reflect.TypeView {
//baml:vm
function as_type(self) -> type throws never { $rust_function }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// A map-kind view of a `type` value. Users never construct this class.
class Type {
implements baml.reflect.TypeView {
//baml:vm
function as_type(self) -> type throws never { $rust_function }
}
//baml:mut_vm
function key_type(self) -> type throws never { $rust_function }
//baml:mut_vm
function value_type(self) -> type throws never { $rust_function }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/// A primitive-kind view of a `type` value. Users never construct this class.
class Type {
implements baml.reflect.TypeView {
//baml:vm
function as_type(self) -> type throws never { $rust_function }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// A union-kind view of a `type` value. Users never construct this class.
class Type {
implements baml.reflect.TypeView {
//baml:vm
function as_type(self) -> type throws never { $rust_function }
}
//baml:mut_vm
function member_types(self) -> type[] throws never { $rust_function }
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ class Arg {
type type
}

/// Metadata baked into reflected schema definitions.
class Meta {
alias string?
description string?
docstring string?
other map<string, string>
}

/// Common identity-preserving surface shared by every reflection kind view.
interface TypeView {
function as_type(self) -> type throws never
}

/// The closed set of reflection views for a runtime `type` value.
type TypeKind = baml.reflect.class.Type | baml.reflect.enum.Type | baml.reflect.union.Type | baml.reflect.literal.Type | baml.reflect.array.Type | baml.reflect.map.Type | baml.reflect.interface.Type | baml.reflect.primitive.Type | baml.reflect.function.Type

/// The runtime signature of a function value, reconstructed from the value
/// itself (BEP-062). Positional (required) parameters appear in `args` in
/// declaration order; named/optional parameters appear in `opts`, keyed by
Expand Down
26 changes: 26 additions & 0 deletions baml_language/crates/baml_builtins2/baml_std/baml/type_class.baml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,32 @@
/// Users never construct `Type` directly; they receive `type` values via
/// `type.of<User>()` and call methods on them.
class TypeValue {
/// Returns the precise kind view of this type value. The view preserves
/// the receiver's mint identity.
//baml:vm
function kind(self) -> baml.reflect.TypeKind throws never {
$rust_function
}

//baml:vm
function as_class(self) -> baml.reflect.class.Type? throws never { $rust_function }
//baml:vm
function as_enum(self) -> baml.reflect.enum.Type? throws never { $rust_function }
//baml:vm
function as_union(self) -> baml.reflect.union.Type? throws never { $rust_function }
//baml:vm
function as_literal(self) -> baml.reflect.literal.Type? throws never { $rust_function }
//baml:vm
function as_array(self) -> baml.reflect.array.Type? throws never { $rust_function }
//baml:vm
function as_map(self) -> baml.reflect.map.Type? throws never { $rust_function }
//baml:vm
function as_interface(self) -> baml.reflect.interface.Type? throws never { $rust_function }
//baml:vm
function as_primitive(self) -> baml.reflect.primitive.Type? throws never { $rust_function }
//baml:vm
function as_function(self) -> baml.reflect.function.Type? throws never { $rust_function }

/// Returns the type name as a human-readable string.
implements baml.ToString {
function to_string(self) -> string throws never {
Expand Down
9 changes: 9 additions & 0 deletions baml_language/crates/baml_builtins2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ pub const ALL: &[BuiltinFile] = &[
builtin!("baml", "ns_random/random.baml"),
// `baml.reflect` (BEP-066 I-9: `reflect` is a keyword shorthand for it).
builtin!("baml", "ns_reflect/reflect.baml"),
builtin!("baml", "ns_reflect/ns_class/class.baml"),
builtin!("baml", "ns_reflect/ns_enum/enum.baml"),
builtin!("baml", "ns_reflect/ns_union/union.baml"),
builtin!("baml", "ns_reflect/ns_literal/literal.baml"),
builtin!("baml", "ns_reflect/ns_array/array.baml"),
builtin!("baml", "ns_reflect/ns_map/map.baml"),
builtin!("baml", "ns_reflect/ns_interface/interface.baml"),
builtin!("baml", "ns_reflect/ns_primitive/primitive.baml"),
builtin!("baml", "ns_reflect/ns_function/function.baml"),
// `baml.type` (BEP-066 K-13: `type.of` / `type.of_value` resolve here).
builtin!("baml", "ns_type/type.baml"),
// --- boundary package ---
Expand Down
Loading
Loading