struct Indexable::View(T, U)

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

Defined in:

views/indexable_view.cr

Constructors

Instance Method Summary

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

def self.new(indexable : T) #

[View source]

Instance Method Detail

def <=>(rhs : Indexable(U)) : Int32 #

[View source]
def <=>(rhs : self) : Int32 #

[View source]
def ==(rhs : Indexable(U)) : Bool #

[View source]
def inspect(io : IO) : Nil #
Description copied from struct Struct

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)"

[View source]
def size : Int32 #
Description copied from module Indexable(U)

Returns the number of elements in this container.


[View source]
def to_s(io : IO) : Nil #
Description copied from struct Struct

Same as #inspect(io).


[View source]
def unsafe_fetch(index : Int) : U #
Description copied from module Indexable(U)

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.


[View source]