1 /**
2     Tuples
3     
4     Copyright:
5         Copyright © 2023-2025, Kitsunebi Games
6         Copyright © 2023-2025, Inochi2D Project
7     
8     License:   $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
9     Authors:
10         Luna Nielsen
11 */
12 module nulib.data.tuple;
13 
14 /**
15     A tuple, essentially a struct constructed from its template
16     arguments.
17 
18     Example:
19         $(D_CODE
20             NamedTuple!(int, "a", int, "b") myTuple;
21             myTuple.a = 42;
22             myTuple.b = 128;
23         )
24 */
25 struct NamedTuple(Args...) if (Args.length > 0) {
26 public:
27 @nogc:
28     static assert(Args.length % 2 == 0, "Tuples must be constructed in the form of Tuple!(Type, \"name\", ...)!");
29     
30     static foreach(i; 0..Args.length/2) {
31         mixin(Args[(i*2)], " ", Args[(i*2)+1], ";");    
32     }
33 }
34 
35 @("NamedTuple")
36 unittest {
37     NamedTuple!(int, "a", int, "b") myTuple = { 1, 2 };
38     assert(myTuple.a == 1);
39     assert(myTuple.b == 2);
40 }
41 
42 /**
43     A tuple which has automatically generated member names.
44     The names are in order of what was passed to the tuple.
45 
46     Example:
47         $(D_CODE
48             Tuple!(int, int) myTuple;
49             myTuple.item1 = 42;
50             myTuple.item2 = 128;
51         )
52 */
53 struct Tuple(Args...) if (Args.length > 0) {
54     import std.conv : text;
55 
56     static foreach(i; 0..Args.length) {
57         mixin(Args[i], " item", (i+1).text, ";");   
58     }
59 }
60 
61 @("Tuple")
62 unittest {
63     Tuple!(int, int) myTuple2 = { 1, 2 };
64     assert(myTuple2.item1 == 1);
65     assert(myTuple2.item2 == 2);
66 }