← nimrepo

macros, generics

17ff1228c2f1e5f8b2526e07f44333c7c09404d8 · ovidiucs · Feb 20, 2019 22:26

+22 −5 · 2 files changed

  • generics_stuff.nim +8 −5
  • macros_stuff.nim +14 −0
diff --git a/generics_stuff.nim b/generics_stuff.nim
index 89fd3f6..8deceaa 100644
--- a/generics_stuff.nim
+++ b/generics_stuff.nim
@@ -1,17 +1,18 @@
 ## About generics:
-## * Generics are Nim's means to parametrize procs
-## * Type safe containers.
+## * Generics are Nim's means to parametrize procs (aka parametric polymorphism or templates in D)
+## * Type safe containers. Types that aren't known until the algoritms are invoked.
 ## * Generics are checked when you instantiate them mostly.
 ## * Generics are duck typed and very macro like.
 ## * Generics are obeying module scope.
 ## * Generics are a type based macro system on top of another macro system.
 ## * Generics are some what similar to c++ templates
 ## * Generics are typechecked after expansion.
-## *  generics are checked at instantiation time
+## * Generics are checked at instantiation time
 ## * Generics are a way to create code that works on a range of type.
 ## * The usage of T within the generic function is what defines the typeclass so to speak.
-## * in general generics are just something like type "variables" : e.g. proc reprT (value: T): 
+## * in general generics are just something like type "variables" : e.g. proc reprT (value: T):
 ##   string makes something with value for every possible T type
+
 proc `+`(a,b: string): string =
     a & b
 
@@ -20,4 +21,6 @@ proc `*`[T](a: T, b: int): T =
     for i in 0 .. b-1:
         result = result + a
 
-echo("a" * 10)
\ No newline at end of file
+echo("a" * 10)
+
+echo ("10" + "10")
diff --git a/macros_stuff.nim b/macros_stuff.nim
new file mode 100644
index 0000000..c992529
--- /dev/null
+++ b/macros_stuff.nim
@@ -0,0 +1,14 @@
+# Copyright 2019.
+
+## About macros:
+## * more advanced then templates.
+## * macros is procedural (it describes how code should be produced at compile time)
+## * templates are declarative (the produced code that should be put in the calling scope is written)
+import macros
+
+dumpTree:
+  for i in 0 .. 10:
+    echo 'a'$i
+
+dumpTree:
+  echo ("10" + "10")