summary refs log tree commit diff stats
path: root/tests/tuples/tuple_subscript.nim
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2017-05-04 16:02:50 +0200
committerAndreas Rumpf <rumpf_a@web.de>2017-05-04 16:02:50 +0200
commit764cc0217735454c166cb7dd490db58f5fa6fe26 (patch)
tree63633b39ae24815e8b677ccbc9bac2cb01c39d3b /tests/tuples/tuple_subscript.nim
parentafa80092d378a6dbc116c0aa3ed3964fd8c599d6 (diff)
parentc1aa973758a60d7ef0e698c94861b74132612de5 (diff)
downloadNim-764cc0217735454c166cb7dd490db58f5fa6fe26.tar.gz
Merge branch 'devel' into araq
Diffstat (limited to 'tests/tuples/tuple_subscript.nim')
-rw-r--r--tests/tuples/tuple_subscript.nim40
1 files changed, 40 insertions, 0 deletions
diff --git a/tests/tuples/tuple_subscript.nim b/tests/tuples/tuple_subscript.nim
new file mode 100644
index 000000000..021793dc3
--- /dev/null
+++ b/tests/tuples/tuple_subscript.nim
@@ -0,0 +1,40 @@
+discard """
+  output: '''5
+5
+str2
+str2
+4'''
+"""
+
+proc`[]` (t: tuple, key: string): string =
+  for name, field in fieldPairs(t):
+    if name == key: 
+      return $field
+  return ""
+
+
+proc`[]` [A,B](t: tuple, key: string, op: (proc(x: A): B)): B =
+  for name, field in fieldPairs(t):
+    when field is A:
+      if name == key: 
+        return op(field)
+
+proc`[]=`[T](t: var tuple, key: string, val: T) =
+  for name, field in fieldPairs(t):
+    when field is T:
+      if name == key: 
+        field = val
+
+var tt = (a: 1, b: "str1")
+
+# test built in operator
+tt[0] = 5
+echo tt[0] 
+echo `[]`(tt, 0)
+
+
+# test overloaded operator
+tt["b"] = "str2"
+echo tt["b"] 
+echo `[]`(tt, "b")
+echo tt["b", proc(s: string) : int = s.len]
\ No newline at end of file