Skip to content

Commit 1f592ec

Browse files
Improve docs on initial/default values passed to Array.new and Hash.new (#13962)
Co-authored-by: Quinton Miller <[email protected]>
1 parent eed093c commit 1f592ec

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

src/array.cr

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,21 @@ class Array(T)
127127
#
128128
# ```
129129
# Array.new(3, 'a') # => ['a', 'a', 'a']
130+
# ```
131+
#
132+
# WARNING: The initial value is filled into the array as-is. It gets neither
133+
# duplicated nor cloned. For types with reference semantics this means every
134+
# item will point to the *same* object.
130135
#
136+
# ```
131137
# ary = Array.new(3, [1])
132138
# ary # => [[1], [1], [1]]
133139
# ary[0][0] = 2
134140
# ary # => [[2], [2], [2]]
135141
# ```
142+
#
143+
# * `.new(Int, & : Int32 -> T)` is an alternative that allows using a
144+
# different initial value for each position.
136145
def initialize(size : Int, value : T)
137146
if size < 0
138147
raise ArgumentError.new("Negative array size: #{size}")

src/hash.cr

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,16 @@ class Hash(K, V)
266266
# Creates a new empty `Hash` with a *block* that handles missing keys.
267267
#
268268
# ```
269+
# inventory = Hash(String, Int32).new(0)
270+
# inventory["socks"] = 3
271+
# inventory["pickles"] # => 0
272+
# ```
273+
#
274+
# WARNING: When the default block is invoked on a missing key, its return
275+
# value is *not* implicitly stored into the hash under that key. If you want
276+
# that behaviour, you need to put it explicitly:
277+
#
278+
# ```
269279
# hash = Hash(String, Int32).new do |hash, key|
270280
# hash[key] = key.size
271281
# end
@@ -294,14 +304,23 @@ class Hash(K, V)
294304
# inventory["pickles"] # => 0
295305
# ```
296306
#
297-
# NOTE: The default value is passed by reference:
307+
# WARNING: When the default value gets returned on a missing key, it is *not*
308+
# stored into the hash under that key. If you want that behaviour, please use
309+
# the overload with a block.
310+
#
311+
# WARNING: The default value is returned as-is. It gets neither duplicated nor
312+
# cloned. For types with reference semantics this means it will be exactly the
313+
# *same* object every time.
314+
#
298315
# ```
299-
# arr = [1, 2, 3]
300-
# hash = Hash(String, Array(Int32)).new(arr)
301-
# hash["3"][1] = 4
302-
# arr # => [1, 4, 3]
316+
# hash = Hash(String, Array(Int32)).new([1])
317+
# hash["a"][0] = 2
318+
# hash["b"] # => [2]
303319
# ```
304320
#
321+
# * `.new(&block : (Hash(K, V), K -> V))` is an alternative with a block that
322+
# can return a different default value for each invocation.
323+
#
305324
# The *initial_capacity* is useful to avoid unnecessary reallocations
306325
# of the internal buffer in case of growth. If the number of elements
307326
# a hash will hold is known, the hash should be initialized with that

0 commit comments

Comments
 (0)