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

Docs: Add references to Number collection convenience constructors #13020

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
23 changes: 23 additions & 0 deletions src/number.cr
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ struct Number
# ints = Int64[1, 2, 3]
# ints.class # => Array(Int64)
# ```
#
# This is similar to an array literal of the same item type:
#
# ```
# Int64[1, 2, 3, 4] # : Array(Int64)
# [1, 2, 3, 4] of Int64 # : Array(Int64)
# ```
macro [](*nums)
Array({{@type}}).build({{nums.size}}) do |%buffer|
{% for num, i in nums %}
Expand All @@ -92,6 +99,14 @@ struct Number
# ints = Int64.slice(1, 2, 3)
# ints.class # => Slice(Int64)
# ```
#
# This is a convenient alternative to `Slice.[]` for designating a
# specific item type which also considers autocasting.
HertzDevil marked this conversation as resolved.
Show resolved Hide resolved
#
# ```
# Int64.slice(1, 2, 3, 4) # : Slice(Int64)
# Slice[1_i64, 2_i64, 3_i64, 4_i64] # : Slice(Int64)
# ```
macro slice(*nums, read_only = false)
%slice = Slice({{@type}}).new({{nums.size}}, read_only: {{read_only}})
{% for num, i in nums %}
Expand All @@ -110,6 +125,14 @@ struct Number
# ints = Int64.static_array(1, 2, 3)
# ints.class # => StaticArray(Int64, 3)
# ```
#
# This is a convenvenient alternative to `StaticArray.[]` for designating a
# specific item type which also considers autocasting.
#
# ```
# Int64.static_array(1, 2, 3, 4) # : StaticArray(Int64)
# StaticArray[1_i64, 2_i64, 3_i64, 4_i64] # : StaticArray(Int64)
# ```
macro static_array(*nums)
%array = uninitialized StaticArray({{@type}}, {{nums.size}})
{% for num, i in nums %}
Expand Down
3 changes: 2 additions & 1 deletion src/slice.cr
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ struct Slice(T)
# If `T` is a `Number` then this is equivalent to
# `Number.slice` (numbers will be coerced to the type `T`)
#
# See also: `Number.slice`.
# * `Number.slice` is a convenient alternative for designating a
# specific numerical item type.
macro [](*args, read_only = false)
# TODO: there should be a better way to check this, probably
# asking if @type was instantiated or if T is defined
Expand Down
3 changes: 2 additions & 1 deletion src/static_array.cr
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ struct StaticArray(T, N)
# ary.class # => StaticArray(Char | Int32, 2)
# ```
#
# See also: `Number.static_array`.
# * `Number.static_array` is a convenient alternative for designating a
# specific numerical item type.
macro [](*args)
%array = uninitialized StaticArray(typeof({{*args}}), {{args.size}})
{% for arg, i in args %}
Expand Down