summary refs log tree commit diff stats
path: root/lib/pure/collections
diff options
context:
space:
mode:
authorFredrik Høisæther Rasch <fredrik.h.rasch@uit.no>2017-10-31 12:23:06 +0100
committerAndreas Rumpf <rumpf_a@web.de>2017-11-18 14:18:00 +0100
commit5dfbeab65f6ba16afed58111b4276244cb1e8d34 (patch)
treea9a5e3b69f80876daf53980e82da742977b0799a /lib/pure/collections
parenta583d686b46b25747fd7b946bb7debf10bde0055 (diff)
downloadNim-5dfbeab65f6ba16afed58111b4276244cb1e8d34.tar.gz
Implement an `asArray` macro
fixes #6563
Diffstat (limited to 'lib/pure/collections')
-rw-r--r--lib/pure/collections/sequtils.nim29
1 files changed, 29 insertions, 0 deletions
diff --git a/lib/pure/collections/sequtils.nim b/lib/pure/collections/sequtils.nim
index 0eb8e6704..6041f8268 100644
--- a/lib/pure/collections/sequtils.nim
+++ b/lib/pure/collections/sequtils.nim
@@ -20,6 +20,8 @@
 
 include "system/inclrtl"
 
+import macros
+
 when not defined(nimhygiene):
   {.pragma: dirty.}
 
@@ -704,6 +706,29 @@ template newSeqWith*(len: int, init: untyped): untyped =
     result[i] = init
   result
 
+macro asArray*(values: typed, targetType: typedesc): untyped =
+  ## converts a static array to an array of type ``targetType`` by converting
+  ## each value in the array to the specified type.
+  ##
+  ## Example:
+  ##
+  ## .. code-block::
+  ##   let x = asArray([0.1, 1.2, 2.3, 3.4], int)
+  ##   doAssert x is array[4, int]
+  ##
+  ## Short notation for:
+  ##
+  ## .. code-block::
+  ##   let x = [(0.1).int, (1.2).int, (2.3).int, (3.4).int]
+  let tNode = getType(targetType)[1]
+  values.expectKind(nnkBracket)
+  result = newNimNode(nnkBracket, lineInfoFrom=values)
+  for i in 0 ..< len(values):
+    var dot = newNimNode(nnkDotExpr, lineInfoFrom=values[i])
+    dot.add newPar(values[i])
+    dot.add tNode
+    result.add dot
+
 when isMainModule:
   import strutils
   block: # concat test
@@ -992,5 +1017,9 @@ when isMainModule:
     seq2D[0][1] = true
     doAssert seq2D == @[@[true, true], @[true, false], @[false, false], @[false, false]]
 
+  block: # asArray tests
+    let x = asArray([1.2, 2.3, 3.4, 4.5], int)
+    doAssert x is array[4, int]
+
   when not defined(testing):
     echo "Finished doc tests"
.subx.html?h=hlt&id=ce2c1efc41470764126e9a1a7f4e0cfec4213587'>ce2c1efc ^
651fc300 ^
6e181e7f ^















ce2c1efc ^
6e181e7f ^















762107fd ^
6e181e7f ^



2104d1a7 ^

9a777801 ^
6e181e7f ^

9a777801 ^
6e181e7f ^






2104d1a7 ^
b1635a5c ^
33352536 ^

e99038ea ^
33352536 ^



9a777801 ^
33352536 ^
9a777801 ^
33352536 ^
e99038ea ^
33352536 ^
e99038ea ^
33352536 ^
651fc300 ^








































6e181e7f ^



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137