Skip to main content

Mojo struct

Dim

@register_passable(trivial)

A static or dynamic dimension modeled with an optional integer.

This class is meant to represent an optional static dimension. When a value is present, the dimension has that static value. When a value is not present, the dimension is dynamic.

Implemented traits

AnyType, Boolable, CollectionElement, Copyable, ImplicitlyBoolable, Intable, Movable, Stringable, Writable

Methods

__init__

__init__[type: Intable](out self, value: type)

Creates a statically-known dimension.

Parameters:

  • type (Intable): The Intable type.

Args:

  • value (type): The static dimension value.

__init__[type: IntLike](out self, value: type)

Creates a statically-known dimension.

Parameters:

  • type (IntLike): The Intable type.

Args:

  • value (type): The static dimension value.

__init__(out self, value: index)

Creates a statically-known dimension.

Args:

  • value (index): The static dimension value.

__init__(out self, value: Int)

Creates a statically-known dimension.

Args:

  • value (Int): The static dimension value.

__init__(out self)

Creates a dynamic dimension with no static value.

__bool__

__bool__(self) -> Bool

Returns True if the dimension has a static value.

Returns:

Whether the dimension has a static value.

__eq__

__eq__(self, rhs: Self) -> Bool

Compares two dimensions for equality.

Args:

  • rhs (Self): The other dimension.

Returns:

True if the dimensions are the same.

__ne__

__ne__(self, rhs: Self) -> Bool

Compare two dimensions for inequality.

Args:

  • rhs (Self): The dimension to compare.

Returns:

True if they are not equal.

__mul__

__mul__(self, rhs: Self) -> Self

Multiplies two dimensions.

If either are unknown, the result is unknown as well.

Args:

  • rhs (Self): The other dimension.

Returns:

The product of the two dimensions.

__floordiv__

__floordiv__(self, rhs: Self) -> Self

Divide by the given dimension and round towards negative infinity.

If either are unknown, the result is unknown as well.

Args:

  • rhs (Self): The divisor dimension.

Returns:

The floor division of the two dimensions.

__rfloordiv__

__rfloordiv__(self, rhs: Self) -> Self

Divide the given argument by self and round towards negative infinity.

If either are unknown, the result is unknown as well.

Args:

  • rhs (Self): The dimension to divide by this Dim.

Returns:

The floor of the argument divided by self.

__imul__

__imul__(inout self, rhs: Self)

Inplace multiplies two dimensions.

If either are unknown, the result is unknown as well.

Args:

  • rhs (Self): The other dimension.

__as_bool__

__as_bool__(self) -> Bool

Returns True if the dimension has a static value.

Returns:

Whether the dimension has a static value.

has_value

has_value(self) -> Bool

Returns True if the dimension has a static value.

Returns:

Whether the dimension has a static value.

is_dynamic

is_dynamic(self) -> Bool

Returns True if the dimension has a dynamic value.

Returns:

Whether the dimension is dynamic.

get

get(self) -> Int

Gets the static dimension value.

Returns:

The static dimension value.

is_multiple

is_multiple[alignment: Int](self) -> Bool

Checks if the dimension is aligned.

Parameters:

  • alignment (Int): The alignment requirement.

Returns:

Whether the dimension is aligned.

__int__

__int__(self) -> Int

Gets the static dimension value.

Returns:

The static dimension value.

__str__

__str__(self) -> String

Converts the Dim to a String. If the value is unknown, then the string "?" is returned.

Returns:

The string representation of the type.

write_to

write_to[W: Writer](self, inout writer: W)

Formats this DimList to the provided Writer.

Parameters:

  • W (Writer): A type conforming to the Writable trait.

Args:

  • writer (W): The object to write to.

or_else

or_else(self, default: Int) -> Int

Return the underlying value contained in the Optional or a default value if the Optional's underlying value is not present.

Args:

  • default (Int): The new value to use if no value was present.

Returns:

The underlying value contained in the Optional or a default value.