Skip to main content

trait

Sized

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

You can pass an instance of Foo to the len() function to get its length:

var foo = Foo(42)
print(len(foo) == 42)
True

If the __len__() method can raise an error, use the SizedRaising trait instead.

Implemented traits

AnyType

Methods

__len__

__len__(self: T) -> Int

Get the length of the type.

Returns:

The length of the type.