Skip to main content
Version: dev

TypeDefinition

std::meta::type_def contains methods on the built-in TypeDefinition type. This type corresponds to struct Name { field1: Type1, ... } and enum Name { Variant1(Fields1), ... } items in the source program.

Methods

add_abi

add_abi
pub comptime fn add_abi(self, abi_argument: CtString) {}

Source code: noir_stdlib/src/meta/type_def.nr#L5-L7

Adds an abi attribute to the data type with the specified argument.

as_type

as_type
pub comptime fn as_type(self) -> Type {}

Source code: noir_stdlib/src/meta/type_def.nr#L12-L14

Returns this type definition as a type in the source program. If this definition has any generics, the generics are also included as-is.

as_type_with_generics

as_type_with_generics
pub comptime fn as_type_with_generics(self, generics: [Type]) -> Option<Type> {}

Source code: noir_stdlib/src/meta/type_def.nr#L23-L25

Returns a type from this type definition using the given generic arguments. Returns Option::none() if an incorrect amount of generic arguments are given for this type.

generics

generics
pub comptime fn generics(self) -> [(Type, Option<Type>)] {}

Source code: noir_stdlib/src/meta/type_def.nr#L44-L46

Returns each generic on this type definition. Each generic is represented as a tuple containing the type, and an optional containing the numeric type if it's a numeric generic.

Example:

#[example]
struct Foo<T, U, let K: u32> {
bar: [T; K],
baz: Baz<U, U>,
}

comptime fn example(foo: TypeDefinition) {
assert_eq(foo.generics().len(), 3);

// Fails because `T` isn't in scope
// let t = quote { T }.as_type();
// assert_eq(foo.generics()[0].0, t);
assert(foo.generics()[0].1.is_none());

// Last generic is numeric, so we have the numeric type available to us
assert(foo.generics()[2].1.is_some());
}

fields

fields
pub comptime fn fields(self, generic_args: [Type]) -> [(Quoted, Type, Quoted)] {}

Source code: noir_stdlib/src/meta/type_def.nr#L52-L54

Returns (name, type, visibility) tuples of each field in this struct type. Any generic types used in each field type is automatically substituted with the provided generic arguments.

fields_as_written

fields_as_written
pub comptime fn fields_as_written(self) -> [(Quoted, Type, Quoted)] {}

Source code: noir_stdlib/src/meta/type_def.nr#L61-L63

Returns (name, type, visibility) tuples of each field in this struct type. Each type is as-is with any generic arguments unchanged. Unless the field types are not needed, users should generally prefer to use TypeDefinition::fields over this function if possible.

has_named_attribute

has_named_attribute
pub comptime fn has_named_attribute<let N: u32>(self, name: str<N>) -> bool {}

Source code: noir_stdlib/src/meta/type_def.nr#L28-L30

Returns true if this type has a custom attribute with the given name.

This matches both built-in attributes and user-written attributes (tags and applied comptime macros). Use has_builtin_attribute if you need to match only the built-in attribute of the given name.

has_builtin_attribute

has_builtin_attribute
pub comptime fn has_builtin_attribute<let N: u32>(self, name: str<N>) -> bool {}

Source code: noir_stdlib/src/meta/type_def.nr#L33-L35

Returns true if this type has a built-in attribute with the given name.

Unlike has_named_attribute, this ignores user-written tag attributes and applied comptime macros.

location

location
pub comptime fn location(self) -> Location {}

Source code: noir_stdlib/src/meta/type_def.nr#L37-L39

Returns the source Location where this type is defined. This can be passed to std::meta::error or std::meta::warn to attach a diagnostic to the type.

module

module
pub comptime fn module(self) -> Module {}

Source code: noir_stdlib/src/meta/type_def.nr#L66-L68

Returns the module where the type is defined.

name

name
pub comptime fn name(self) -> Quoted {}

Source code: noir_stdlib/src/meta/type_def.nr#L71-L73

Returns the name of this type

Note that the returned quoted value will be just the type name, it will not be the full path to the type definition, nor will it include any generics.

Trait Implementations

impl Eq for TypeDefinition
impl Hash for TypeDefinition

Note that each type definition is assigned a unique ID internally and this is what is used for equality and hashing. So even type definitions with identical generics and fields may not be equal in this sense if they were originally different items in the source program.