def __getitem__(self, key):
        """Slice the underlying value.

        A field corresponding to :py:`key` is looked up in the layout. If the field's shape is
        a shape-castable object, returns the result of calling :meth:`~.ShapeCastable.from_bits`.
        Otherwise, returns an :class:`int`.

        Arguments
        ---------
        key : :class:`str` or :class:`int` or :class:`.ValueCastable`
            Name or index of a field.

        Returns
        -------
        unspecified type or :class:`int`
            A slice of the underlying value defined by the field.

        Raises
        ------
        :exc:`KeyError`
            If the layout does not define a field corresponding to :py:`key`.
        :exc:`TypeError`
            If :py:`key` is a value-castable object, but the layout of the constant is not
            an :class:`ArrayLayout`.
        :exc:`Exception`
            If the bit pattern of the field is not valid according to
            :meth:`.ShapeCastable.from_bits`. Usually this will be a :exc:`ValueError`.
        """
        if isinstance(self.__layout, ArrayLayout):
            elem_width = Shape.cast(self.__layout.elem_shape).width
            if isinstance(key, slice):
                start, stop, stride = key.indices(self.__layout.length)
                shape = ArrayLayout(self.__layout.elem_shape, len(range(start, stop, stride)))
                if stride == 1:
                    value = (self.__target >> start * elem_width) & ((1 << elem_width * (stop - start)) - 1)
                else:
                    value = 0
                    pos = 0
                    for index in range(start, stop, stride):
                        elem_value = (self.__target >> index * elem_width) & ((1 << elem_width) - 1)
                        value |= elem_value << pos
                        pos += elem_width
            elif isinstance(key, int):
                if key not in range(-self.__layout.length, self.__layout.length):
                    raise IndexError(f"Index {key} is out of range for array layout of length "
                                     f"{self.__layout.length}")
                if key < 0:
                    key += self.__layout.length
                shape = self.__layout.elem_shape
                value = (self.__target >> key * elem_width) & ((1 << elem_width) - 1)
            elif isinstance(key, (Value, ValueCastable)):
                return View(self.__layout, self.as_value())[key]
            else:
                raise TypeError(
                    f"Constant with array layout may only be indexed with an integer or a value, "
                    f"not {key!r}")
        else:
            if isinstance(key, slice):
                raise TypeError(
                    "Non-array constant cannot be indexed with a slice; did you mean to call "
                    "`.as_value()` first?")
            if isinstance(key, (Value, ValueCastable)):
                raise TypeError(
                    f"Only constants with array layout, not {self.__layout!r}, may be indexed with "
                    f"a value")
            field = self.__layout[key]
            shape = field.shape
            value = (self.__target >> field.offset) & ((1 << field.width) - 1)
        # Field guarantees that the shape-castable object is well-formed, so there is no need
        # to handle erroneous cases here.
        if isinstance(shape, ShapeCastable):
            return shape.from_bits(value)
        return hdl.Const(value, Shape.cast(shape)).value
Slice the underlying value.

        A field corresponding to :py:`key` is looked up in the layout. If the field's shape is
        a shape-castable object, returns the result of calling :meth:`~.ShapeCastable.from_bits`.
        Otherwise, returns an :class:`int`.

        Arguments
        ---------
        key : :class:`str` or :class:`int` or :class:`.ValueCastable`
            Name or index of a field.

        Returns
        -------
        unspecified type or :class:`int`
            A slice of the underlying value defined by the field.

        Raises
        ------
        :exc:`KeyError`
            If the layout does not define a field corresponding to :py:`key`.
        :exc:`TypeError`
            If :py:`key` is a value-castable object, but the layout of the constant is not
            an :class:`ArrayLayout`.
        :exc:`Exception`
            If the bit pattern of the field is not valid according to
            :meth:`.ShapeCastable.from_bits`. Usually this will be a :exc:`ValueError`.
        
amaranth/lib/data.py