mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-19 05:50:30 +00:00
explicit firstElement for $ in collections
This commit is contained in:
@@ -1653,11 +1653,13 @@ proc `$`*[T: tuple|object](x: T): string =
|
||||
## $(23, 45) == "(23, 45)"
|
||||
## $() == "()"
|
||||
result = "("
|
||||
var firstElement = true
|
||||
for name, value in fieldPairs(x):
|
||||
if result.len > 1: result.add(", ")
|
||||
if not(firstElement): result.add(", ")
|
||||
result.add(name)
|
||||
result.add(": ")
|
||||
result.add($value)
|
||||
firstElement = false
|
||||
result.add(")")
|
||||
|
||||
proc `$`*[T: set](x: T): string =
|
||||
@@ -1667,9 +1669,11 @@ proc `$`*[T: set](x: T): string =
|
||||
## .. code-block:: nimrod
|
||||
## ${23, 45} == "{23, 45}"
|
||||
result = "{"
|
||||
var firstElement = true
|
||||
for value in items(x):
|
||||
if result.len > 1: result.add(", ")
|
||||
if not(firstElement): result.add(", ")
|
||||
result.add($value)
|
||||
firstElement = false
|
||||
result.add("}")
|
||||
|
||||
proc `$`*[T: seq](x: T): string =
|
||||
@@ -1679,9 +1683,11 @@ proc `$`*[T: seq](x: T): string =
|
||||
## .. code-block:: nimrod
|
||||
## $(@[23, 45]) == "@[23, 45]"
|
||||
result = "@["
|
||||
var firstElement = true
|
||||
for value in items(x):
|
||||
if result.len > 2: result.add(", ")
|
||||
if not(firstElement): result.add(", ")
|
||||
result.add($value)
|
||||
firstElement = false
|
||||
result.add("]")
|
||||
|
||||
when false:
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
discard """
|
||||
output:'''@[23, 45]'''
|
||||
output:'''@[23, 45]
|
||||
@[, \"foo\", \"bar\"]'''
|
||||
"""
|
||||
|
||||
echo($(@[23, 45]))
|
||||
echo($(@["", "foo", "bar"]))
|
||||
|
||||
Reference in New Issue
Block a user