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_attribute
pub comptime fn add_attribute<let N: u32>(self, attribute: str<N>) {}

Source code: noir_stdlib/src/meta/type_def.nr#L6-L8

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#L18-L20

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#L29-L31

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#L41-L43

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#L49-L51

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#L58-L60

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#L34-L36

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

module

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

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

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#L68-L70

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.