crystal-lang / crystal

The Crystal Programming Language
https://crystal-lang.org
Apache License 2.0
19.36k stars 1.62k forks source link

`Indexable#find` and `#find!` with start offsets #14642

Open HertzDevil opened 4 months ago

HertzDevil commented 4 months ago

Indexable has overloads of Enumerable#index and #index! that accept an additional optional starting offset argument:

module Enumerable(T)
  def index(& : T ->) : Int32?; end
  def index(obj) : Int32?; end
  def index!(& : T ->) : Int32; end
  def index!(obj) : Int32; end
end

module Indexable(T)
  def index(offset : Int = 0, & : T ->); end
  def index(object, offset : Int = 0); end
  def index!(offset : Int = 0, & : T ->); end
  def index!(obj, offset : Int = 0); end
end

The same is not true for Enumerable#find:

module Enumerable(T)
  def find(if_none = nil, & : T ->); end
  def find!(& : T ->) : T; end
end

module Indexable(T)
  # no overloads for `#find` and `#find!`
end

If x : Indexable then it seems natural that:

So I think those additional overloads should be available in Indexable as well, so that we don't have to write the above unsafe_fetch (or even a regular #[] call).

fabricerenard12 commented 3 months ago

Hi! I'm a first time contributor to crystal and would like to try implementing this feature. Could you assign the issue to me please?