summary refs log tree commit diff stats
path: root/lib/pure/strformat.nim
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2018-01-12 01:27:24 +0100
committerAraq <rumpf_a@web.de>2018-01-12 01:27:24 +0100
commit66634fe1a0cf4956a82315299b4fc43b855b3ab5 (patch)
tree6629baf39266bccd4b383659868a30f38e27bb6e /lib/pure/strformat.nim
parent425f221440cbd453591d0fca9eaf93671d5f6d62 (diff)
downloadNim-66634fe1a0cf4956a82315299b4fc43b855b3ab5.tar.gz
strformat: introduce 'fmt' as an alias for '%'; ensure overloading resolution produces a clash between strformat.'%' and json.'%'
Diffstat (limited to 'lib/pure/strformat.nim')
-rw-r--r--lib/pure/strformat.nim19
1 files changed, 18 insertions, 1 deletions
diff --git a/lib/pure/strformat.nim b/lib/pure/strformat.nim
index 0673c4fa8..c771343c3 100644
--- a/lib/pure/strformat.nim
+++ b/lib/pure/strformat.nim
@@ -226,7 +226,7 @@ template callFormatOption(res, arg, option) {.dirty.} =
   else:
     format($arg, option, res)
 
-macro `%`*(pattern: string{lit}): untyped =
+macro `%`*(pattern: string): untyped =
   ## For a specification of the ``%`` macro, see the module level documentation.
   runnableExamples:
     template check(actual, expected: string) =
@@ -407,6 +407,18 @@ macro `%`*(pattern: string{lit}): untyped =
   when defined(debugFmtDsl):
     echo repr result
 
+template fmt*(pattern: string): untyped =
+  ## An alias for ``%``. Helps to avoid conflicts with ``json``'s ``%`` operator.
+  ## **Examples:**
+  ##
+  ## .. code-block:: nim
+  ##  import json
+  ##  import strformat except `%`
+  ##
+  ##  let example = "oh, look no conflicts anymore"
+  ##  echo fmt"{example}"
+  %pattern
+
 proc mkDigit(v: int, typ: char): string {.inline.} =
   assert(v < 26)
   if v < 10:
@@ -618,3 +630,8 @@ proc format*(value: string; specifier: string; res: var string) =
       "invalid type in format string for string, expected 's', but got " &
       spec.typ)
   res.add alignString(value, spec.minimumWidth, spec.align, spec.fill)
+
+when isMainModule:
+  import json
+
+  doAssert fmt"{'a'} {'b'}" == "a b"