This repository was archived by the owner on Oct 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 417
[WIP] core.array.staticArray static array litteral: staticArray(1, 2, 3)
#2093
Closed
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1e6c101
core.array.StaticArray
timotheecour 478eac0
fixup
timotheecour e51f829
fixup
timotheecour 111642b
added asStatic
timotheecour 462779a
added asStatic!(2.iota)
timotheecour 0c58d16
fixup
timotheecour 4f79a3b
fixup
timotheecour 7580600
fixup
timotheecour bcf1ee3
refactor
timotheecour 2e140ea
fixup
timotheecour 2a3ca26
fixup
timotheecour 69d0176
fixup
timotheecour 588ed37
fixup
timotheecour File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
module core.array; | ||
|
||
import core.internal.traits : Unqual, CommonType; | ||
|
||
debug import std.stdio; | ||
|
||
// @nogc: https://issues.dlang.org/show_bug.cgi?id=18439 | ||
nothrow @safe pure: | ||
|
||
/++ | ||
Returns a static array constructed from `a`. The type of elements can be | ||
specified implicitly (`int[2] a = staticArray(1,2);`) or explicitly | ||
(`float[2] a = staticArray!float(1,2)`). | ||
D20180214T203015: The result is an rvalue, therefore uses like | ||
`foo(staticArray(1, 2, 3)` may be inefficient because of the copies. | ||
+/ | ||
pragma(inline, true) U[T.length] staticArray(U = CommonType!T, T...)(T a) @nogc | ||
{ | ||
return [a]; | ||
} | ||
|
||
// D20180214T185602: Workaround https://issues.dlang.org/show_bug.cgi?id=16779 (make alias to staticArray once fixed) | ||
pragma(inline, true) U[T.length] staticArrayCast(U, T...)(T a) @nogc | ||
{ | ||
enum n = T.length; | ||
U[n] ret = void; | ||
static foreach (i; 0 .. n) | ||
cast(Unqual!U)(ret[i]) = cast(U)(a[i]); | ||
return ret; | ||
} | ||
|
||
/// | ||
unittest | ||
{ | ||
{ | ||
auto a = staticArray(1, 2, 3); | ||
assert(is(typeof(a) == int[3])); | ||
assert(a == [1, 2, 3]); | ||
} | ||
{ | ||
auto a = staticArray(1, 2.0); | ||
assert(is(typeof(a) == double[2])); | ||
assert(a == [1, 2.0]); | ||
} | ||
assert(!__traits(compiles, staticArray(1, ""))); | ||
assert(is(typeof(staticArray()) == void[0])); | ||
|
||
{ | ||
auto a = staticArray!float(1, 2); | ||
assert(is(typeof(a) == float[2])); | ||
assert(a == [1, 2]); | ||
|
||
} | ||
{ | ||
// see D20180214T185602 | ||
// auto a = staticArray!byte(1, 2); | ||
auto a = staticArrayCast!byte(1, 2); | ||
assert(is(typeof(a) == byte[2])); | ||
assert(a == [1, 2]); | ||
} | ||
{ | ||
auto a = staticArrayCast!byte(1, 129); | ||
assert(a == [1, -127]); | ||
} | ||
|
||
assert(is(typeof(staticArray([1])) == int[][1])); | ||
|
||
// NOTE: correctly issues a deprecation | ||
// int[] a2 = staticArray(1,2); | ||
} | ||
|
||
/++ | ||
Returns a static array constructed from `arr`. The type of elements can be | ||
specified implicitly (`int[2] a = [1,2].asStatic;`) or explicitly | ||
(`float[2] a = [1,2].asStatic!float`). | ||
See also D20180214T203015. | ||
+/ | ||
pragma(inline, true) T[n] asStatic(T, size_t n)(auto ref T[n] arr) @nogc | ||
{ | ||
return arr; | ||
} | ||
|
||
//U[n] asStatic(U, T, size_t n)(auto ref T[n] arr) @nogc if (!is(U == T) && is(T : U)) | ||
U[n] asStatic(U, T, size_t n)(auto ref T[n] arr) @nogc if (!is(U == T)) | ||
{ | ||
U[n] ret = void; | ||
static foreach (i; 0 .. n) | ||
cast(Unqual!U)(ret[i]) = arr[i]; | ||
return ret; | ||
} | ||
|
||
/// ditto | ||
auto asStatic(U = typeof(arr[0]), alias arr)() @nogc | ||
{ | ||
enum n = arr.length; | ||
U[n] ret = void; | ||
static foreach (i; 0 .. n) | ||
cast(Unqual!U)(ret[i]) = arr[i]; | ||
return ret; | ||
} | ||
|
||
/// ditto | ||
auto asStatic(alias arr)() @nogc | ||
{ | ||
enum n = arr.length; | ||
alias U = typeof(arr[0]); | ||
U[n] ret = void; | ||
static foreach (i; 0 .. n) | ||
cast(Unqual!U)(ret[i]) = arr[i]; | ||
return ret; | ||
} | ||
|
||
/// | ||
unittest | ||
{ | ||
{ | ||
auto a = [1, 2, 3].asStatic; | ||
assert(is(typeof(a) == int[3])); | ||
assert(a == [1, 2, 3]); | ||
} | ||
|
||
@nogc void checkNogc() | ||
{ | ||
auto a = [1, 2, 3].asStatic; | ||
assert(a == [1, 2, 3]); | ||
} | ||
|
||
{ | ||
auto a = [1, 2, 3].asStatic!double; | ||
} | ||
|
||
{ | ||
auto a = [1, 2, 3].asStatic!int; | ||
// https://issues.dlang.org/show_bug.cgi?id=16779 | ||
//auto a2 = [1, 2, 3].asStatic!byte; | ||
//auto a3 = [1, 2, 3].asStatic!ubyte; | ||
} | ||
{ | ||
auto a1 = [1, 2, 3].asStatic!(const(int)); | ||
const(int)[3] a2 = [1, 2, 3].asStatic; | ||
auto a3 = [1, 2, 3].asStatic!(const(double)); | ||
} | ||
|
||
{ | ||
import std.range; | ||
|
||
enum a = asStatic!(double, 2.iota); | ||
assert(is(typeof(a) == double[2])); | ||
assert(a == [0, 1]); | ||
} | ||
{ | ||
import std.range; | ||
|
||
enum a = asStatic!(2.iota); | ||
assert(is(typeof(a) == int[2])); | ||
assert(a == [0, 1]); | ||
} | ||
|
||
// NOTE: correctly issues a deprecation | ||
//int[] a2 = [1,2].asStatic; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simply create new unittest blocks instead of scoping. It doesn't hurt and allows the user of the documentation to hit "run" and "edit" for an individual block. Plus you can give a short summary title in
/// Amazing example
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pushing back on this request as well, but happy to revisit if you have good arguments