Mojo trait
UIntSized
The Sized
trait describes a type that has an integer length (such as a string or array).
Any type that conforms to Sized
or
SizedRaising
works with the
built-in len()
function.
The Sized
trait requires a type to implement the __len__()
method. For example:
@value
struct Foo(Sized):
var length: Int
fn __len__(self) -> Int:
return self.length
@value
struct Foo(Sized):
var length: Int
fn __len__(self) -> Int:
return self.length
You can pass an instance of Foo
to the len()
function to get its
length:
var foo = Foo(42)
print(len(foo) == 42)
var foo = Foo(42)
print(len(foo) == 42)
True
True
Note: If the __len__()
method can raise an error, use the
SizedRaising
trait instead.
Implemented traits
AnyType
,
UnknownDestructibility
Methods
__len__
__len__(self: _Self) -> UInt
Get the length of the type.
Returns:
The length of the type.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!