Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add return type restrictions to methods of container types #10582

Merged
merged 6 commits into from
Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 29 additions & 29 deletions src/array.cr
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ class Array(T)
# a[6..10]? # => nil
# a[6..]? # => nil
# ```
def []?(range : Range)
def []?(range : Range) : Array(T)?
self[*Indexable.range_to_index_and_count(range, size) || return nil]?
end

Expand Down Expand Up @@ -670,7 +670,7 @@ class Array(T)
# a = ["a", "b", "c", "d", "e"]
# a.clear # => []
# ```
def clear
def clear : self
@buffer.clear(@size)
@size = 0
self
Expand Down Expand Up @@ -723,7 +723,7 @@ class Array(T)
# ary.compact!
# ary # => ["a", "b", "c"]
# ```
def compact!
def compact! : self
reject! &.nil?
end

Expand Down Expand Up @@ -781,7 +781,7 @@ class Array(T)
# a.delete("x") # => nil
# a # => ["a", "c"]
# ```
def delete(obj)
def delete(obj) : T?
internal_delete { |e| e == obj }[1]
end

Expand Down Expand Up @@ -819,7 +819,7 @@ class Array(T)
# a # => ["ant", "dog"]
# a.delete_at(99..100) # raises IndexError
# ```
def delete_at(range : Range)
def delete_at(range : Range) : self
index, count = Indexable.range_to_index_and_count(range, self.size) || raise IndexError.new
delete_at(index, count)
end
Expand All @@ -835,7 +835,7 @@ class Array(T)
# a # => ["ant", "dog"]
# a.delete_at(99, 1) # raises IndexError
# ```
def delete_at(index : Int, count : Int)
def delete_at(index : Int, count : Int) : self
index += size if index < 0
unless 0 <= index <= size
raise IndexError.new
Expand Down Expand Up @@ -1044,7 +1044,7 @@ class Array(T)
# [1, 2, 3].first(2) # => [1, 2]
# [1, 2, 3].first(4) # => [1, 2, 3]
# ```
def first(n : Int)
def first(n : Int) : Array(T)
self[0, n]
end

Expand Down Expand Up @@ -1092,7 +1092,7 @@ class Array(T)
# [1, 2, 3].last(2) # => [2, 3]
# [1, 2, 3].last(4) # => [1, 2, 3]
# ```
def last(n : Int)
def last(n : Int) : Array(T)
if n < @size
self[@size - n, n]
else
Expand Down Expand Up @@ -1147,7 +1147,7 @@ class Array(T)
# ```
#
# See also: `Array#reject!`.
def select!(pattern)
def select!(pattern) : self
self.select! { |elem| pattern === elem }
end

Expand Down Expand Up @@ -1176,7 +1176,7 @@ class Array(T)
# ```
#
# See also: `Array#select!`.
def reject!(pattern)
def reject!(pattern) : self
reject! { |elem| pattern === elem }
self
end
Expand Down Expand Up @@ -1315,7 +1315,7 @@ class Array(T)
end
end

def repeated_permutations(size : Int = self.size)
def repeated_permutations(size : Int = self.size) : Array(Array(T))
ary = [] of Array(T)
each_repeated_permutation(size) do |a|
ary << a
Expand Down Expand Up @@ -1344,7 +1344,7 @@ class Array(T)
# a.pop # => "c"
# a # => ["a", "b"]
# ```
def pop
def pop : T
pop { raise IndexError.new }
end

Expand Down Expand Up @@ -1389,7 +1389,7 @@ class Array(T)
# a.pop(4) # => ["a", "b", "c"]
# a # => []
# ```
def pop(n : Int)
def pop(n : Int) : Array(T)
if n < 0
raise ArgumentError.new("Can't pop negative count")
end
Expand All @@ -1404,7 +1404,7 @@ class Array(T)
end

# Like `pop`, but returns `nil` if `self` is empty.
def pop?
def pop? : T?
pop { nil }
end

Expand Down Expand Up @@ -1448,7 +1448,7 @@ class Array(T)
# a = ["a"]
# a.push("b", "c") # => ["a", "b", "c"]
# ```
def push(*values : T)
def push(*values : T) : self
new_size = @size + values.size

resize_if_cant_insert(values.size)
Expand All @@ -1460,7 +1460,7 @@ class Array(T)
self
end

def replace(other : Array)
def replace(other : Array) : self
@size = other.size
resize_to_capacity(Math.pw2ceil(@size)) if @size > @capacity
@buffer.copy_from(other.to_unsafe, other.size)
Expand All @@ -1473,7 +1473,7 @@ class Array(T)
# a = [1, 2, 3]
# a.reverse # => [3, 2, 1]
# ```
def reverse
def reverse : Array(T)
Array(T).new(size) { |i| @buffer[size - i - 1] }
end

Expand All @@ -1498,7 +1498,7 @@ class Array(T)
# a2 # => [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
# a3 # => [3, 4, 5, 6, 7, 8, 9, 0, 1, 2]
# ```
def rotate!(n = 1)
def rotate!(n = 1) : self
return self if size == 0
n %= size

Expand Down Expand Up @@ -1542,7 +1542,7 @@ class Array(T)
# a.rotate(3) # => [3, 4, 5, 6, 7, 8, 9, 0, 1, 2]
# a # => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# ```
def rotate(n = 1)
def rotate(n = 1) : Array(T)
return self if size == 0
n %= size
return self if n == 0
Expand All @@ -1561,7 +1561,7 @@ class Array(T)
# a.shift # => "a"
# a # => ["b", "c"]
# ```
def shift
def shift : T
shift { raise IndexError.new }
end

Expand Down Expand Up @@ -1614,7 +1614,7 @@ class Array(T)
# a.shift(4) # => ["a", "b", "c"]
# a # => []
# ```
def shift(n : Int)
def shift(n : Int) : Array(T)
if n < 0
raise ArgumentError.new("Can't shift negative count")
end
Expand Down Expand Up @@ -1647,19 +1647,19 @@ class Array(T)
# a.shift? # => nil
# a # => []
# ```
def shift?
def shift? : T?
shift { nil }
end

# Returns an array with all the elements in the collection randomized
# using the given *random* number generator.
def shuffle(random = Random::DEFAULT)
def shuffle(random = Random::DEFAULT) : Array(T)
dup.shuffle!(random)
end

# Modifies `self` by randomizing the order of elements in the collection
# using the given *random* number generator. Returns `self`.
def shuffle!(random = Random::DEFAULT)
def shuffle!(random = Random::DEFAULT) : self
@buffer.shuffle!(size, random)
self
end
Expand Down Expand Up @@ -1894,7 +1894,7 @@ class Array(T)
# a.uniq! # => ["a", "b", "c"]
# a # => ["a", "b", "c"]
# ```
def uniq!
def uniq! : self
if size <= 1
return self
end
Expand Down Expand Up @@ -1959,7 +1959,7 @@ class Array(T)
# a.unshift("c") # => ["c", "a", "b"]
# a.unshift(1) # => [1, "c", "a", "b"]
# ```
def unshift(object : T)
def unshift(object : T) : self
# If we have no more room left before the beginning of the array
# we make the array larger, but point the buffer to start at the middle
# of the entire allocated memory. In this way, if more elements are unshift
Expand All @@ -1980,7 +1980,7 @@ class Array(T)

# Prepend multiple values. The same as `unshift`, but takes an arbitrary number
# of values to add to the array. Returns `self`.
def unshift(*values : T)
def unshift(*values : T) : self
values.reverse_each do |value|
unshift(value)
end
Expand Down Expand Up @@ -2060,7 +2060,7 @@ class Array(T)
@size == remaining_capacity
end

def remaining_capacity
def remaining_capacity : Int32
@capacity - @offset_to_buffer
end

Expand Down Expand Up @@ -2142,7 +2142,7 @@ class Array(T)
end

# :nodoc:
def index(object, offset : Int = 0)
def index(object, offset : Int = 0) : Int32?
# Optimize for the case of looking for a byte in a byte slice
if T.is_a?(UInt8.class) &&
(object.is_a?(UInt8) || (object.is_a?(Int) && 0 <= object < 256))
Expand Down
18 changes: 9 additions & 9 deletions src/atomic.cr
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ struct Atomic(T)
# atomic.add(2) # => 1
# atomic.get # => 3
# ```
def add(value : T)
def add(value : T) : T
Ops.atomicrmw(:add, pointerof(@value), value, :sequentially_consistent, false)
end

Expand All @@ -63,7 +63,7 @@ struct Atomic(T)
# atomic.sub(2) # => 9
# atomic.get # => 7
# ```
def sub(value : T)
def sub(value : T) : T
Ops.atomicrmw(:sub, pointerof(@value), value, :sequentially_consistent, false)
end

Expand All @@ -74,7 +74,7 @@ struct Atomic(T)
# atomic.and(3) # => 5
# atomic.get # => 1
# ```
def and(value : T)
def and(value : T) : T
Ops.atomicrmw(:and, pointerof(@value), value, :sequentially_consistent, false)
end

Expand All @@ -85,7 +85,7 @@ struct Atomic(T)
# atomic.nand(3) # => 5
# atomic.get # => -2
# ```
def nand(value : T)
def nand(value : T) : T
Ops.atomicrmw(:nand, pointerof(@value), value, :sequentially_consistent, false)
end

Expand All @@ -96,7 +96,7 @@ struct Atomic(T)
# atomic.or(2) # => 5
# atomic.get # => 7
# ```
def or(value : T)
def or(value : T) : T
Ops.atomicrmw(:or, pointerof(@value), value, :sequentially_consistent, false)
end

Expand All @@ -107,7 +107,7 @@ struct Atomic(T)
# atomic.xor(3) # => 5
# atomic.get # => 6
# ```
def xor(value : T)
def xor(value : T) : T
Ops.atomicrmw(:xor, pointerof(@value), value, :sequentially_consistent, false)
end

Expand Down Expand Up @@ -172,7 +172,7 @@ struct Atomic(T)
# atomic.set(10) # => 10
# atomic.get # => 10
# ```
def set(value : T)
def set(value : T) : T
Ops.store(pointerof(@value), value.as(T), :sequentially_consistent, true)
value
end
Expand All @@ -184,11 +184,11 @@ struct Atomic(T)
# atomic.lazy_set(10) # => 10
# atomic.get # => 10
# ```
def lazy_set(@value : T)
def lazy_set(@value : T) : T
end

# Atomically returns this atomic's value.
def get
def get : T
Ops.load(pointerof(@value), :sequentially_consistent, true)
end

Expand Down
6 changes: 3 additions & 3 deletions src/bit_array.cr
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct BitArray
return LibC.memcmp(@bits, other.@bits, bytesize) == 0
end

def unsafe_fetch(index : Int)
def unsafe_fetch(index : Int) : Bool
bit_index, sub_index = index.divmod(32)
(@bits[bit_index] & (1 << sub_index)) > 0
end
Expand Down Expand Up @@ -84,7 +84,7 @@ struct BitArray
# ba[5..10] # => BitArray[]
# ba[-2...-1] # => BitArray[0]
# ```
def [](range : Range)
def [](range : Range) : BitArray
self[*Indexable.range_to_index_and_count(range, size) || raise IndexError.new]
end

Expand All @@ -109,7 +109,7 @@ struct BitArray
# ba[1, 2] # => BitArray[01]
# ba[5, 1] # => BitArray[]
# ```
def [](start : Int, count : Int)
def [](start : Int, count : Int) : BitArray
raise ArgumentError.new "Negative count: #{count}" if count < 0

if start == size
Expand Down
Loading