struct Indexable::View(T, U)
- Indexable::View(T, U)
- Struct
- Value
- Object
Overview
An IndexableView
is a read-only version of any indexable type. This
is useful to give read access to a mutable indexable type such as an
array in a type-safe manner.
The functionality of IndexableView
is limited by design, as it just
implements the Indexable
mixin requirements by delegating to the
enclosed object.
arr = StaticArray[1, 2, 3, 4]
view = IndexableView(StaticArray(Int32, 4)).new(arr)
view.size # => 4
view[2] # => 3
view.select &.even? # => [2, 4]
view.select! &.even? # does not compile
view.sort # does not compile
Included Modules
- Comparable(Indexable(U))
- Indexable(U)
Defined in:
views/indexable_view.crConstructors
Instance Method Summary
- #<=>(rhs : Indexable(U)) : Int32
- #<=>(rhs : self) : Int32
- #==(rhs : Indexable(U)) : Bool
-
#inspect(io : IO) : Nil
Appends this struct's name and instance variables names and values to the given IO.
-
#size : Int32
Returns the number of elements in this container.
-
#to_s(io : IO) : Nil
Same as
#inspect(io)
. -
#unsafe_fetch(index : Int) : U
Returns the element at the given index, without doing any bounds check.
Instance methods inherited from module Indexable(U)
[](index : Int, other : Int, *indexes : Int) : self[](indexes : Tuple) : Tuple
[](indexes : Indexable(Int)) : self
[](indexes : Enumerable(Int)) : self [], view : View(self, T) view
Constructor Detail
Instance Method Detail
Appends this struct's name and instance variables names and values to the given IO.
struct Point
def initialize(@x : Int32, @y : Int32)
end
end
p1 = Point.new 1, 2
p1.to_s # "Point(@x=1, @y=2)"
p1.inspect # "Point(@x=1, @y=2)"
Returns the number of elements in this container.
Returns the element at the given index, without doing any bounds check.
Indexable
makes sure to invoke this method with index in 0...size
,
so converting negative indices to positive ones is not needed here.
Clients never invoke this method directly. Instead, they access
elements with #[](index)
and #[]?(index)
.
This method should only be directly invoked if you are absolutely sure the index is in bounds, to avoid a bounds check for a small boost of performance.