summary refs log tree commit diff stats
path: root/lib/pure/strformat.nim
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2019-04-14 18:28:55 +0200
committerAndreas Rumpf <rumpf_a@web.de>2019-04-15 08:20:28 +0200
commit59ccaa43c733c33c9d0db3c8e9a5fd303909482f (patch)
tree0969a08fca270ef8cb69738a60f709be567e4e9b /lib/pure/strformat.nim
parent499fa3f3dc822eb0895116db84e3c63746d4e1a2 (diff)
downloadNim-59ccaa43c733c33c9d0db3c8e9a5fd303909482f.tar.gz
fixes #11012
Diffstat (limited to 'lib/pure/strformat.nim')
-rw-r--r--lib/pure/strformat.nim38
1 files changed, 10 insertions, 28 deletions
diff --git a/lib/pure/strformat.nim b/lib/pure/strformat.nim
index a8ceecf3d..e12ca3c89 100644
--- a/lib/pure/strformat.nim
+++ b/lib/pure/strformat.nim
@@ -102,7 +102,9 @@ of ``formatValue`` procs. The required signature for a type ``T`` that supports
 formatting is usually ``proc formatValue(result: var string; x: T; specifier: string)``.
 
 The subexpression after the colon
-(``arg`` in ``&"{key} is {value:arg} {{z}}"``) is optional. It will be passed as the last argument to ``formatValue``. When the colon with the subexpression it is left out, an empty string will be taken instead.
+(``arg`` in ``&"{key} is {value:arg} {{z}}"``) is optional. It will be passed as
+the last argument to ``formatValue``. When the colon with the subexpression it is
+left out, an empty string will be taken instead.
 
 For strings and numeric types the optional argument is a so-called
 "standard format specifier".
@@ -215,8 +217,7 @@ Limitations
 ===========
 
 Because of the well defined order how templates and macros are
-expanded, strformat cannot expand template arguments.
-
+expanded, strformat cannot expand template arguments:
 
 .. code-block:: nim
   template myTemplate(arg: untyped): untyped =
@@ -233,27 +234,6 @@ quoted string literal. It is not an identifier yet. Then the strformat
 macro creates the ``arg`` identifier from the string literal. An
 identifier that cannot be resolved anymore.
 
-.. code-block:: nim
-  let x = "abc"
-  myTemplate(x)
-
-  # expansion of myTemplate
-
-  let x = "abc"
-  echo "arg is: ", x
-  echo &"--- {arg} ---"
-
-  # expansion of `&`
-
-  let x = "abc"
-  echo "arg is: ", x
-  echo:
-    var temp = newStringOfCap(20)
-    temp.add "--- "
-    temp.formatValue arg, "" # arg cannot be resolved anymore
-    temp.add " ---"
-    temp
-
 The workaround for this is to bind template argument to a new local variable.
 
 .. code-block:: nim
@@ -265,11 +245,12 @@ The workaround for this is to bind template argument to a new local variable.
       echo &"--- {arg1} ---"
 
 The use of ``{.inject.}`` here is necessary again because of template
-expansion order and hygienic templates. But since we generelly want to
+expansion order and hygienic templates. But since we generally want to
 keep the hygienicness of ``myTemplate``, and we do not want ``arg1``
 to be injected into the context where ``myTemplate`` is expanded,
 everything is wrapped in a ``block``.
 
+
 Future directions
 =================
 
@@ -522,8 +503,9 @@ proc formatValue*(result: var string; value: string; specifier: string) =
       setLen(value, runeOffset(value, spec.precision))
   result.add alignString(value, spec.minimumWidth, spec.align, spec.fill)
 
-template formatValue[T: enum](result: var string; value: T; specifier: string) =
-  result.add $value
+proc formatValue[T](result: var string; value: T; specifier: string) =
+  mixin `$`
+  formatValue(result, $value, specifier)
 
 template formatValue(result: var string; value: char; specifier: string) =
   result.add value
@@ -531,7 +513,7 @@ template formatValue(result: var string; value: char; specifier: string) =
 template formatValue(result: var string; value: cstring; specifier: string) =
   result.add value
 
-proc formatValue[T](result: var string; value: openarray[T]; specifier: string) =
+proc formatValue(result: var string; value: (array|seq|openArray); specifier: string) =
   result.add "["
   for i, it in value:
     if i != 0: