1"use strict";(globalThis.webpackChunkdocs=globalThis.webpackChunkdocs||[]).push([[628],{26851(e,t,n){n.r(t),n.d(t,{assets:()=>o,contentTitle:()=>a,default:()=>p,frontMatter:()=>l,metadata:()=>r,toc:()=>c});const r=JSON.parse('{"id":"libraries/standard_library/meta/typ","title":"Type","description":"Represent and analyze types at compile time\u2014query structure, check trait bounds, and resolve trait impls.","source":"@site/versioned_docs/version-v1.0.0-beta.26/libraries/standard_library/meta/typ.md","sourceDirName":"libraries/standard_library/meta","slug":"/libraries/standard_library/meta/typ","permalink":"/docs/v1.0.0-beta.26/libraries/standard_library/meta/typ","draft":false,"unlisted":false,"editUrl":"https://github.com/noir-lang/noir/edit/master/docs/versioned_docs/version-v1.0.0-beta.26/libraries/standard_library/meta/typ.md","tags":[],"version":"v1.0.0-beta.26","frontMatter":{"title":"Type","description":"Represent and analyze types at compile time\u2014query structure, check trait bounds, and resolve trait impls."},"sidebar":"sidebar","previous":{"title":"TraitImpl","permalink":"/docs/v1.0.0-beta.26/libraries/standard_library/meta/trait_impl"},"next":{"title":"TypeDefinition","permalink":"/docs/v1.0.0-beta.26/libraries/standard_library/meta/type_def"}}');var s=n(74848),i=n(28453);const l={title:"Type",description:"Represent and analyze types at compile time\u2014query structure, check trait bounds, and resolve trait impls."},a=void 0,o={},c=[{value:"Functions",id:"functions",level:2},{value:"Methods",id:"methods",level:2},{value:"as_array",id:"as_array",level:3},{value:"as_constant",id:"as_constant",level:3},{value:"as_integer",id:"as_integer",level:3},{value:"as_mutable_reference",id:"as_mutable_reference",level:3},{value:"as_vector",id:"as_vector",level:3},{value:"as_str",id:"as_str",level:3},{value:"as_data_type",id:"as_data_type",level:3},{value:"as_tuple",id:"as_tuple",level:3},{value:"get_trait_impl",id:"get_trait_impl",level:3},{value:"implements",id:"implements",level:3},{value:"is_bool",id:"is_bool",level:3},{value:"is_field",id:"is_field",level:3},{value:"is_unit",id:"is_unit",level:3},{value:"Trait Implementations",id:"trait-implementations",level:2}];function d(e){const t={a:"a",blockquote:"blockquote",code:"code",h2:"h2",h3:"h3",p:"p",pre:"pre",sub:"sub",sup:"sup",...(0,i.R)(),...e.components};return(0,s.jsxs)(s.Fragment,{children:[(0,s.jsxs)(t.p,{children:[(0,s.jsx)(t.code,{children:"std::meta::typ"})," contains methods on the built-in ",(0,s.jsx)(t.code,{children:"Type"})," type used for representing\na type in the source program."]}),"\n",(0,s.jsx)(t.h2,{id:"functions",children:"Functions"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-rust",metastring:'title="fresh_type_variable" showLineNumbers ',children:"pub comptime fn fresh_type_variable() -> Type {}\n"})}),"\n",(0,s.jsxs)(t.blockquote,{children:["\n",(0,s.jsx)(t.p,{children:(0,s.jsx)(t.sup,{children:(0,s.jsx)(t.sub,{children:(0,s.jsx)(t.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/typ.nr#L57-L59",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/typ.nr#L57-L59"})})})}),"\n"]}),"\n",(0,s.jsxs)(t.p,{children:["Creates and returns an unbound type variable. This is a special kind of type internal\nto type checking which will type check with any other type. When it is type checked\nagainst another type it will also be set to that type. For example, if ",(0,s.jsx)(t.code,{children:"a"})," is a type\nvariable and we have the type equality ",(0,s.jsx)(t.code,{children:"(a, i32) = (u8, i32)"}),", the compiler will set\n",(0,s.jsx)(t.code,{children:"a"})," equal to ",(0,s.jsx)(t.code,{children:"u8"}),"."]}),"\n",(0,s.jsxs)(t.p,{children:["Unbound type variables will often be rendered as ",(0,s.jsx)(t.code,{children:"_"})," while printing them. Bound type\nvariables will appear as the type they are bound to."]}),"\n",(0,s.jsxs)(t.p,{children:["This can be used in conjunction with functions which internally perform type checks\nsuch as ",(0,s.jsx)(t.code,{children:"Type::implements"})," or ",(0,s.jsx)(t.code,{children:"Type::get_trait_impl"})," to potentially grab some of the types used."]}),"\n",(0,s.jsxs)(t.p,{children:["Note that calling ",(0,s.jsx)(t.code,{children:"Type::implements"})," or ",(0,s.jsx)(t.code,{children:"Type::get_trait_impl"})," on a type variable will always\nfail."]}),"\n",(0,s.jsx)(t.p,{children:"Example:"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-rust",metastring:'title="serialize-setup" showLineNumbers ',children:"trait Serialize<let N: u32> {}\n\nimpl Serialize<1> for Field {}\n\nimpl<T, let N: u32, let M: u32> Serialize<N * M> for [T; N]\nwhere\n T: Serialize<M>,\n{}\n\nimpl<T, U, let N: u32, let M: u32> Serialize<N + M> for (T, U)\nwhere\n T: Serialize<N>,\n U: Serialize<M>,\n{}\n"})}),"\n",(0,s.jsxs)(t.blockquote,{children:["\n",(0,s.jsx)(t.p,{children:(0,s.jsx)(t.sup,{children:(0,s.jsx)(t.sub,{children:(0,s.jsx)(t.a,{href:"https://github.com/noir-lang/noir/blob/master/test_programs/compile_success_empty/comptime_type/src/main.nr#L14-L29",target:"_blank",rel:"noopener noreferrer",children:"Source code: test_programs/compile_success_empty/comptime_type/src/main.nr#L14-L29"})})})}),"\n"]}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-rust",metastring:'title="fresh-type-variable-example" showLineNumbers ',children:'let typevar1 = std::meta::typ::fresh_type_variable();\nlet constraint = quote { Serialize<$typevar1> }.as_trait_constraint();\nlet field_type = quote { Field }.as_type();\n\n// Search for a trait impl (binding typevar1 to 1 when the impl is found):\nassert(field_type.implements(constraint));\n\n// typevar1 should be bound to the "1" generic now:\nassert_eq(typevar1.as_constant().unwrap(), 1);\n\n// If we want to do the same with a different type, we need to\n// create a new type variable now that `typevar1` is bound\nlet t
1ypevar2 = std::meta::typ::fresh_type_variable();\nlet constraint = quote { Serialize<$typevar2> }.as_trait_constraint();\nlet array_type = quote { [(Field, Field); 5] }.as_type();\nassert(array_type.implements(constraint));\n\n// Now typevar2 should be bound to the serialized pair size 2 times the array length 5\nassert_eq(typevar2.as_constant().unwrap(), 10);\n'})}),"\n",(0,s.jsxs)(t.blockquote,{children:["\n",(0,s.jsx)(t.p,{children:(0,s.jsx)(t.sup,{children:(0,s.jsx)(t.sub,{children:(0,s.jsx)(t.a,{href:"https://github.com/noir-lang/noir/blob/master/test_programs/compile_success_empty/comptime_type/src/main.nr#L137-L157",target:"_blank",rel:"noopener noreferrer",children:"Source code: test_programs/compile_success_empty/comptime_type/src/main.nr#L137-L157"})})})}),"\n"]}),"\n",(0,s.jsx)(t.h2,{id:"methods",children:"Methods"}),"\n",(0,s.jsx)(t.h3,{id:"as_array",children:"as_array"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-rust",metastring:'title="as_array" showLineNumbers ',children:"pub comptime fn as_array(self) -> Option<(Type, Type)> {}\n"})}),"\n",(0,s.jsxs)(t.blockquote,{children:["\n",(0,s.jsx)(t.p,{children:(0,s.jsx)(t.sup,{children:(0,s.jsx)(t.sub,{children:(0,s.jsx)(t.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/typ.nr#L76-L78",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/typ.nr#L76-L78"})})})}),"\n"]}),"\n",(0,s.jsx)(t.p,{children:"If this type is an array, return a pair of (element type, size type)."}),"\n",(0,s.jsx)(t.p,{children:"Example:"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-rust",children:"comptime {\n let array_type = quote { [Field; 3] }.as_type();\n let (field_type, three_type) = array_type.as_array().unwrap();\n\n assert(field_type.is_field());\n assert_eq(three_type.as_constant().unwrap(), 3);\n}\n"})}),"\n",(0,s.jsx)(t.h3,{id:"as_constant",children:"as_constant"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-rust",metastring:'title="as_constant" showLineNumbers ',children:"pub comptime fn as_constant(self) -> Option<u32> {}\n"})}),"\n",(0,s.jsxs)(t.blockquote,{children:["\n",(0,s.jsx)(t.p,{children:(0,s.jsx)(t.sup,{children:(0,s.jsx)(t.sub,{children:(0,s.jsx)(t.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/typ.nr#L83-L85",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/typ.nr#L83-L85"})})})}),"\n"]}),"\n",(0,s.jsxs)(t.p,{children:["If this type is a constant integer (such as the ",(0,s.jsx)(t.code,{children:"3"})," in the array type ",(0,s.jsx)(t.code,{children:"[Field; 3]"}),"),\nreturn the numeric constant."]}),"\n",(0,s.jsx)(t.h3,{id:"as_integer",children:"as_integer"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-rust",metastring:'title="as_integer" showLineNumbers ',children:"pub comptime fn as_integer(self) -> Option<(bool, u8)> {}\n"})}),"\n",(0,s.jsxs)(t.blockquote,{children:["\n",(0,s.jsx)(t.p,{children:(0,s.jsx)(t.sup,{children:(0,s.jsx)(t.sub,{children:(0,s.jsx)(t.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/typ.nr#L90-L92",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/typ.nr#L90-L92"})})})}),"\n"]}),"\n",(0,s.jsxs)(t.p,{children:["If this is an integer type, return a boolean which is ",(0,s.jsx)(t.code,{children:"true"}),"\nif the type is signed, as well as the number of bits of this integer type."]}),"\n",(0,s.jsx)(t.h3,{id:"as_mutable_reference",children:"as_mutable_reference"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-rust",metastring:'title="as_mutable_reference" showLineNumbers ',children:"pub comptime fn as_mutable_reference(self) -> Option<Type> {}\n"})}),"\n",(0,s.jsxs)(t.blockquote,{children:["\n",(0,s.jsx)(t.p,{children:(0,s.jsx)(t.sup,{children:(0,s.jsx)(t.sub,{children:(0,s.jsx)(t.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/typ.nr#L96-L98",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/typ.nr#L96-L98"})})})}),"\n"]}),"\n",(0,s.jsxs)(t.p,{children:["If this is a mutable reference type ",(0,s.jsx)(t.code,{children:"&mut T"}),", returns the mutable type ",(0,s.jsx)(t.code,{children:"T"}),"."]}),"\n",(0,s.jsx)(t.h3,{id:"as_vector",children:"as_vector"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-rust",metastring:'title="as_vector" showLineNumbers ',children:"pub comptime fn as_vector(self) -> Option<Type> {}\n"})}),"\n",(0,s.jsxs)(t.blockquote,{children:["\n",(0,s.jsx)(t.p,{children:(0,s.jsx)(t.sup,{children:(0,s.jsx)(t.sub,{children:(0,s.jsx)(t.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/typ.nr#L102-L104",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/typ.nr#L102-L104"})})})}),"\n"]}),"\n",(0,s.jsx)(t.p,{children:"If this is a vector type, return the element type of the vector."}),"\n",(0,s.jsx)(t.h3,{id:"as_str",children:"as_str"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-rust",metastring:'title="as_str" showLineNumbers ',children:"pub comptime fn as_str(self) -> Option<Type> {}\n"})}),"\n",(0,s.jsxs)(t.blockquote,{children:["\n",(0,s.jsx)(t.p,{children:(0,s.jsx)(t.sup,{children:(0,s.jsx)(t.sub,{children:(0,s.jsx)(t.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/typ.nr#L108-L110",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/typ.nr#L108-L110"})})})}),"\n"]}),"\n",(0,s.jsxs)(t.p,{children:["If this is a ",(0,s.jsx)(t.code,{children:"str<N>"})," type, returns the length ",(0,s.jsx)(t.code,{children:"N"})," as a type."]}),"\n",(0,s.jsx)(t.h3,{id:"as_data_type",children:"as_data_type"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-rust",metastring:'title="as_data_type" showLineNumbers ',children:"pub comptime fn as_data_type(self) -> Option<(TypeDefinition, [Type])> {}\n"})}),"\n",(0,s.jsxs)(t.blockquote,{children:["\n",(0,s.jsx)(t.p,{children:(0,s.jsx)(t.sup,{children:(0,s.jsx)(t.sub,{children:(0,s.jsx)(t.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/typ.nr#L119-L121",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/typ.nr#L119-L121"})})})}),"\n"]}),"\n",(0,s.jsx)(t.p,{children:"If this is a struct or enum type, returns the type definition in addition to\nany generic arguments on this type."}),"\n",(0,s.jsx)(t.h3,{id:"as_tuple",children:"as_tuple"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-rust",metastring:'title="as_tuple" showLineNumbers ',children:"pub comptime fn as_tuple(self) -> Option<[Type]> {}\n"})}),"\n",(0,s.jsxs)(t.blockquote,{children:["\n",(0,s.jsx)(t.p,{children:(0,s.jsx)(t.sup,{children:(0,s.jsx)(t.sub,{children:(0,s.jsx)(t.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/typ.nr#L125-L127",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/typ.nr#L125-L127"})})})}),"\n"]}),"\n",(0,s.jsx)(t.p,{children:"If this is a tuple type, returns each element type of the tuple."}),"\n",(0,s.jsx)(t.h3,{id:"get_trait_impl",children:"get_trait_impl"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-rust",metastring:'title="get_trait_impl" showLineNumbers ',children:"pub comptime fn get_trait_impl(self, constraint: TraitConstraint) -> Option<TraitImpl> {}\n"})}),"\n",(0,s.jsxs)(t.blockquote,{children:["\n",(0,s.jsx)(t.p,{children:(0,s.jsx)(t.sup,{children:(0,s.jsx)(t.sub,{children:(0,s.jsx)(t.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/typ.nr#L148-L150",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/typ.nr#L148-L150"})})})}),"\n"]}),"\n",(0,s.jsxs)(t.p,{children:["Retrieves the trait implementation that implements the given\ntrait constraint for this type. If the trait constraint is not\nfound, ",(0,s.jsx)(t.code,{children:"None"})," is returned. Note that since the concrete trait implementation\nfor a trait constraint specified in a ",(0,s.jsx)(t.code,{children:"where"})," clause is unknown,\nthis function will return ",(0,s.jsx)(t.code,{children:"None"})," in these cases. If you only want to know\nwhether a type implements a trait, use ",(0,s.jsx)(t.code,{children:"implements"})," instead."]}),"\n",(0,s.jsx)(t.p,{children:"Example:"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-rust",children:"comptime {\n let field_type = quote { Field }.as_type();\n let default = quote { Default }.as_trait_constraint();\n\n let the_impl: TraitImpl = field_type.get_trait_impl(default).unwrap();\n assert(the_impl.methods().len(), 1);\n}\n"})}),"\n",(0,s.jsx)(t.h3,{id:"implements",children:"implements"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-rust",metastring:'title="implements" showLineNumbers ',children:"pub comptime fn implements(self, constraint: TraitConstraint) -> bool {}\n"})}),"\n",(0,s.jsxs)(t.blockquote,{children:["\n",(0,s.jsx)(t.p,{children:(0,s.jsx)(t.sup,{children:(0,s.jsx)(t.sub,{children:(0,s.jsx)(t.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/typ.nr#L171-L173",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/typ.nr#L171-L173"})})})}),"\n"]}),"\n",(0,s.jsxs)(t.p,{children:[(0,s.jsx)(t.code,{children:"true"})," if this type implements the given trait. Note that unlike\n",(0,s.jsx)(t.code,{children:"get_trait_impl"})," this will also return true for any ",(0,s.jsx)(t.code,{children:"where"})," constraints\nin scope."]}),"\n",(0,s.jsx)(t.p,{children:"Example:"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-rust",children:"fn foo<T>() where T: Default {\n comptime {\n let field_type = quote { Field }.as_type();\n let default = quote { Default }.as_trait_constraint();\n assert(field_type.implements(default));\n\n let t = quote { T }.as_type();\n assert(t.implements(default));\n }\n}\n"})}),"\n",(0,s.jsx)(t.h3,{id:"is_bool",children:"is_bool"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-rust",metastring:'title="is_bool" showLineNumbers ',children:"pub comptime fn is_bool(self) -> bool {}\n"})}),"\n",(0,s.jsxs)(t.blockquote,{children:["\n",(0,s.jsx)(t.p,{children:(0,s.jsx)(t.sup,{children:(0,s.jsx)(t.sub,{children:(0,s.jsx)(t.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/typ.nr#L177-L179",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/typ.nr#L177-L179"})})})}),"\n"]}),"\n",(0,s.jsxs)(t.p,{children:[(0,s.jsx)(t.code,{children:"true"})," if this type is ",(0,s.jsx)(t.code,{children:"bool"}),"."]}),"\n",(0,s.jsx)(t.h3,{id:"is_field",children:"is_field"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-rust",metastring:'title="is_field" showLineNumbers ',children:"pub comptime fn is_field(self) -> bool {}\n"})}),"\n",(0,s.jsxs)(t.blockquote,{children:["\n",(0,s.jsx)(t.p,{children:(0,s.jsx)(t.sup,{children:(0,s.jsx)(t.sub,{children:(0,s.jsx)(t.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/typ.nr#L183-L185",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/typ.nr#L183-L185"})})})}),"\n"]}),"\n",(0,s.jsxs)(t.p,{children:[(0,s.jsx)(t.code,{children:"true"})," if this type is ",(0,s.jsx)(t.code,{children:"Field"}),"."]}),"\n",(0,s.jsx)(t.h3,{id:"is_unit",children:"is_unit"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-rust",metastring:'title="is_unit" showLineNumbers ',children:"pub comptime fn is_unit(self) -> bool {}\n"})}),"\n",(0,s.jsxs)(t.blockquote,{children:["\n",(0,s.jsx)(t.p,{children:(0,s.jsx)(t.sup,{children:(0,s.jsx)(t.sub,{children:(0,s.jsx)(t.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/typ.nr#L189-L191",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/typ.nr#L189-L191"})})})}),"\n"]}),"\n",(0,s.jsxs)(t.p,{children:[(0,s.jsx)(t.code,{children:"true"}
1)," if this type is the unit ",(0,s.jsx)(t.code,{children:"()"})," type."]}),"\n",(0,s.jsx)(t.h2,{id:"trait-implementations",children:"Trait Implementations"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-rust",children:"impl Eq for Type\nimpl Hash for Type\n"})}),"\n",(0,s.jsxs)(t.p,{children:["Note that this is syntactic equality, this is not the same as whether two types will type check\nto be the same type. Unless type inference or generics are being used however, users should not\ntypically have to worry about this distinction unless ",(0,s.jsx)(t.code,{children:"std::meta::typ::fresh_type_variable"})," is used."]})]})}function p(e={}){const{wrapper:t}={...(0,i.R)(),...e.components};return t?(0,s.jsx)(t,{...e,children:(0,s.jsx)(d,{...e})}):d(e)}},28453(e,t,n){n.d(t,{R:()=>l,x:()=>a});var r=n(96540);const s={},i=r.createContext(s);function l(e){const t=r.useContext(i);return r.useMemo(function(){return"function"==typeof e?e(t):{...t,...e}},[t,e])}function a(e){let t;return t=e.disableParentContext?"function"==typeof e.components?e.components(s):e.components||s:l(e.components),r.createElement(i.Provider,{value:t},e.children)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.