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 types to methods with defaults #12837

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/array.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1542,7 +1542,7 @@ class Array(T)

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

Expand Down
8 changes: 4 additions & 4 deletions src/char.cr
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ struct Char
# 'x'.downcase # => 'x'
# '.'.downcase # => '.'
# ```
def downcase(options = Unicode::CaseOptions::None) : Char
def downcase(options : Unicode::CaseOptions = :none) : Char
Unicode.downcase(self, options)
end

Expand All @@ -409,7 +409,7 @@ struct Char
# This method takes into account the possibility that an downcase
# version of a char might result in multiple chars, like for
# 'İ', which results in 'i' and a dot mark.
def downcase(options = Unicode::CaseOptions::None)
def downcase(options : Unicode::CaseOptions = :none)
Unicode.downcase(self, options) { |char| yield char }
end

Expand All @@ -427,7 +427,7 @@ struct Char
# 'X'.upcase # => 'X'
# '.'.upcase # => '.'
# ```
def upcase(options = Unicode::CaseOptions::None) : Char
def upcase(options : Unicode::CaseOptions = :none) : Char
Unicode.upcase(self, options)
end

Expand All @@ -441,7 +441,7 @@ struct Char
# 'z'.upcase { |v| puts v } # prints 'Z'
# 'ffl'.upcase { |v| puts v } # prints 'F', 'F', 'L'
# ```
def upcase(options = Unicode::CaseOptions::None)
def upcase(options : Unicode::CaseOptions = :none)
Unicode.upcase(self, options) { |char| yield char }
end

Expand Down
4 changes: 2 additions & 2 deletions src/enumerable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1389,7 +1389,7 @@ module Enumerable(T)
# {1, 2, 3, 4, 5}.sample(2) # => [3, 4]
# {1, 2, 3, 4, 5}.sample(2, Random.new(1)) # => [1, 5]
# ```
def sample(n : Int, random = Random::DEFAULT) : Array(T)
def sample(n : Int, random : Random = Random::DEFAULT) : Array(T)
raise ArgumentError.new("Can't sample negative number of elements") if n < 0

# Unweighted reservoir sampling:
Expand Down Expand Up @@ -1425,7 +1425,7 @@ module Enumerable(T)
# a.sample # => 1
# a.sample(Random.new(1)) # => 3
# ```
def sample(random = Random::DEFAULT) : T
def sample(random : Random = Random::DEFAULT) : T
value = uninitialized T
found = false

Expand Down
4 changes: 2 additions & 2 deletions src/indexable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ module Indexable(T)
# a.sample # => 1
# a.sample(Random.new(1)) # => 2
# ```
def sample(random = Random::DEFAULT)
def sample(random : Random = Random::DEFAULT)
raise IndexError.new("Can't sample empty collection") if size == 0
unsafe_fetch(random.rand(size))
end
Expand All @@ -890,7 +890,7 @@ module Indexable(T)
# If `self` is not empty and `n` is equal to 1, calls `sample(random)` exactly
# once. Thus, *random* will be left in a different state compared to the
# implementation in `Enumerable`.
def sample(n : Int, random = Random::DEFAULT) : Array(T)
def sample(n : Int, random : Random = Random::DEFAULT) : Array(T)
return super unless n == 1

if empty?
Expand Down
2 changes: 1 addition & 1 deletion src/indexable/mutable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ module Indexable::Mutable(T)
# a.shuffle!(Random.new(42)) # => [3, 2, 4, 5, 1]
# a # => [3, 2, 4, 5, 1]
# ```
def shuffle!(random = Random::DEFAULT) : self
def shuffle!(random : Random = Random::DEFAULT) : self
(size - 1).downto(1) do |i|
j = random.rand(i + 1)
swap(i, j)
Expand Down
2 changes: 1 addition & 1 deletion src/range.cr
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ struct Range(B, E)
# the method simply calls `random.rand(self)`.
#
# Raises `ArgumentError` if `self` is an open range.
def sample(random = Random::DEFAULT)
def sample(random : Random = Random::DEFAULT)
{% if B == Nil || E == Nil %}
{% raise "Can't sample an open range" %}
{% end %}
Expand Down
2 changes: 1 addition & 1 deletion src/slice.cr
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ struct Slice(T)
# :inherit:
#
# Raises if this slice is read-only.
def shuffle!(random = Random::DEFAULT) : self
def shuffle!(random : Random = Random::DEFAULT) : self
check_writable
super
end
Expand Down
2 changes: 1 addition & 1 deletion src/string.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3035,7 +3035,7 @@ class String
# ```
#
# Case-sensitive only comparison is provided by the comparison operator `#<=>`.
def compare(other : String, case_insensitive = false, options = Unicode::CaseOptions::None) : Int32
def compare(other : String, case_insensitive = false, options : Unicode::CaseOptions = :none) : Int32
return self <=> other unless case_insensitive

if single_byte_optimizable? && other.single_byte_optimizable?
Expand Down
10 changes: 5 additions & 5 deletions src/uuid.cr
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ struct UUID

# Creates UUID from 16-bytes slice. Raises if *slice* isn't 16 bytes long. See
# `#initialize` for *variant* and *version*.
def self.new(slice : Slice(UInt8), variant = nil, version = nil)
def self.new(slice : Slice(UInt8), variant : Variant? = nil, version : Version? = nil)
raise ArgumentError.new "Invalid bytes length #{slice.size}, expected 16" unless slice.size == 16

bytes = uninitialized UInt8[16]
Expand All @@ -71,14 +71,14 @@ struct UUID

# Creates another `UUID` which is a copy of *uuid*, but allows overriding
# *variant* or *version*.
def self.new(uuid : UUID, variant = nil, version = nil)
def self.new(uuid : UUID, variant : Variant? = nil, version : Version? = nil)
new(uuid.bytes, variant, version)
end

# Creates new UUID by decoding `value` string from hyphenated (ie `ba714f86-cac6-42c7-8956-bcf5105e1b81`),
# hexstring (ie `89370a4ab66440c8add39e06f2bb6af6`) or URN (ie `urn:uuid:3f9eaf9e-cdb0-45cc-8ecb-0e5b2bfb0c20`)
# format, raising an `ArgumentError` if the string does not match any of these formats.
def self.new(value : String, variant = nil, version = nil)
def self.new(value : String, variant : Variant? = nil, version : Version? = nil)
bytes = uninitialized UInt8[16]

case value.size
Expand Down Expand Up @@ -110,7 +110,7 @@ struct UUID
# Creates new UUID by decoding `value` string from hyphenated (ie `ba714f86-cac6-42c7-8956-bcf5105e1b81`),
# hexstring (ie `89370a4ab66440c8add39e06f2bb6af6`) or URN (ie `urn:uuid:3f9eaf9e-cdb0-45cc-8ecb-0e5b2bfb0c20`)
# format, returning `nil` if the string does not match any of these formats.
def self.parse?(value : String, variant = nil, version = nil) : UUID?
def self.parse?(value : String, variant : Variant? = nil, version : Version? = nil) : UUID?
bytes = uninitialized UInt8[16]

case value.size
Expand Down Expand Up @@ -167,7 +167,7 @@ struct UUID
#
# It is strongly recommended to use a cryptographically random source for
# *random*, such as `Random::Secure`.
def self.random(random = Random::Secure, variant = Variant::RFC4122, version = Version::V4) : self
def self.random(random : Random = Random::Secure, variant : Variant = :rfc4122, version : Version = :v4) : self
new_bytes = uninitialized UInt8[16]
random.random_bytes(new_bytes.to_slice)

Expand Down