Skip to content

Commit

Permalink
Docs: Add references to Number collection convenience constructors (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota authored Feb 2, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 7d4f9b2 commit 10d0c73
Showing 3 changed files with 27 additions and 2 deletions.
23 changes: 23 additions & 0 deletions src/number.cr
Original file line number Diff line number Diff line change
@@ -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 %}
@@ -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.
#
# ```
# 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 %}
@@ -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 %}
3 changes: 2 additions & 1 deletion src/slice.cr
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion src/static_array.cr
Original file line number Diff line number Diff line change
@@ -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 %}

0 comments on commit 10d0c73

Please sign in to comment.