move system.dollars in a separate file (#10829)
This commit is contained in:
parent
b270917de1
commit
84d3f3d448
9 changed files with 223 additions and 212 deletions
|
|
@ -10,10 +10,6 @@
|
||||||
## This module defines compile-time reflection procs for
|
## This module defines compile-time reflection procs for
|
||||||
## working with types.
|
## working with types.
|
||||||
|
|
||||||
include "system/helpers" # for `isNamedTuple`
|
|
||||||
|
|
||||||
export system.`$`
|
|
||||||
export isNamedTuple
|
|
||||||
|
|
||||||
proc name*(t: typedesc): string {.magic: "TypeTrait".}
|
proc name*(t: typedesc): string {.magic: "TypeTrait".}
|
||||||
## Returns the name of the given type.
|
## Returns the name of the given type.
|
||||||
|
|
@ -63,6 +59,20 @@ proc supportsCopyMem*(t: typedesc): bool {.magic: "TypeTrait".}
|
||||||
##
|
##
|
||||||
## Other languages name a type like these `blob`:idx:.
|
## Other languages name a type like these `blob`:idx:.
|
||||||
|
|
||||||
|
proc isNamedTuple*(T: typedesc): bool =
|
||||||
|
## Return true for named tuples, false for any other type.
|
||||||
|
when T isnot tuple: result = false
|
||||||
|
else:
|
||||||
|
var t: T
|
||||||
|
for name, _ in t.fieldPairs:
|
||||||
|
when name == "Field0":
|
||||||
|
return compiles(t.Field0)
|
||||||
|
else:
|
||||||
|
return true
|
||||||
|
# empty tuple should be un-named,
|
||||||
|
# see https://github.com/nim-lang/Nim/issues/8861#issue-356631191
|
||||||
|
return false
|
||||||
|
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
static:
|
static:
|
||||||
|
|
|
||||||
213
lib/system.nim
213
lib/system.nim
|
|
@ -2444,62 +2444,6 @@ when not defined(js) and not defined(booting) and defined(nimTrMacros):
|
||||||
# unnecessary slow down in this case.
|
# unnecessary slow down in this case.
|
||||||
swap(cast[ptr pointer](addr arr[a])[], cast[ptr pointer](addr arr[b])[])
|
swap(cast[ptr pointer](addr arr[a])[], cast[ptr pointer](addr arr[b])[])
|
||||||
|
|
||||||
proc `$`*(x: int): string {.magic: "IntToStr", noSideEffect.}
|
|
||||||
## The stringify operator for an integer argument. Returns `x`
|
|
||||||
## converted to a decimal string. ``$`` is Nim's general way of
|
|
||||||
## spelling `toString`:idx:.
|
|
||||||
|
|
||||||
proc `$`*(x: int64): string {.magic: "Int64ToStr", noSideEffect.}
|
|
||||||
## The stringify operator for an `int64` argument. Returns `x`
|
|
||||||
## converted to a decimal string.
|
|
||||||
|
|
||||||
when not defined(nimscript):
|
|
||||||
when not defined(JS) and hasAlloc:
|
|
||||||
proc `$`*(x: uint64): string {.noSideEffect.}
|
|
||||||
## The stringify operator for an unsigned integer argument. Returns `x`
|
|
||||||
## converted to a decimal string.
|
|
||||||
|
|
||||||
proc `$`*(x: float): string {.magic: "FloatToStr", noSideEffect.}
|
|
||||||
## The stringify operator for a float argument. Returns `x`
|
|
||||||
## converted to a decimal string.
|
|
||||||
|
|
||||||
proc `$`*(x: bool): string {.magic: "BoolToStr", noSideEffect.}
|
|
||||||
## The stringify operator for a boolean argument. Returns `x`
|
|
||||||
## converted to the string "false" or "true".
|
|
||||||
|
|
||||||
proc `$`*(x: char): string {.magic: "CharToStr", noSideEffect.}
|
|
||||||
## The stringify operator for a character argument. Returns `x`
|
|
||||||
## converted to a string.
|
|
||||||
##
|
|
||||||
## .. code-block:: Nim
|
|
||||||
## assert $'c' == "c"
|
|
||||||
|
|
||||||
proc `$`*(x: cstring): string {.magic: "CStrToStr", noSideEffect.}
|
|
||||||
## The stringify operator for a CString argument. Returns `x`
|
|
||||||
## converted to a string.
|
|
||||||
|
|
||||||
proc `$`*(x: string): string {.magic: "StrToStr", noSideEffect.}
|
|
||||||
## The stringify operator for a string argument. Returns `x`
|
|
||||||
## as it is. This operator is useful for generic code, so
|
|
||||||
## that ``$expr`` also works if ``expr`` is already a string.
|
|
||||||
|
|
||||||
proc `$`*[Enum: enum](x: Enum): string {.magic: "EnumToStr", noSideEffect.}
|
|
||||||
## The stringify operator for an enumeration argument. This works for
|
|
||||||
## any enumeration type thanks to compiler magic.
|
|
||||||
##
|
|
||||||
## If a ``$`` operator for a concrete enumeration is provided, this is
|
|
||||||
## used instead. (In other words: *Overwriting* is possible.)
|
|
||||||
|
|
||||||
proc `$`*(t: typedesc): string {.magic: "TypeTrait".} =
|
|
||||||
## Returns the name of the given type.
|
|
||||||
##
|
|
||||||
## For more procedures dealing with ``typedesc``, see
|
|
||||||
## `typetraits module <typetraits.html>`_.
|
|
||||||
runnableExamples:
|
|
||||||
doAssert $(type(42)) == "int"
|
|
||||||
doAssert $(type("Foo")) == "string"
|
|
||||||
static: doAssert $(type(@['A', 'B'])) == "seq[char]"
|
|
||||||
|
|
||||||
|
|
||||||
# undocumented:
|
# undocumented:
|
||||||
proc getRefcount*[T](x: ref T): int {.importc: "getRefcount", noSideEffect,
|
proc getRefcount*[T](x: ref T): int {.importc: "getRefcount", noSideEffect,
|
||||||
|
|
@ -2527,17 +2471,6 @@ const
|
||||||
## Note that you cannot compare a floating point value to this value
|
## Note that you cannot compare a floating point value to this value
|
||||||
## and expect a reasonable result - use the `classify` procedure
|
## and expect a reasonable result - use the `classify` procedure
|
||||||
## in the `math module <math.html>`_ for checking for NaN.
|
## in the `math module <math.html>`_ for checking for NaN.
|
||||||
NimMajor* {.intdefine.}: int = 0
|
|
||||||
## The major number of Nim's version.
|
|
||||||
|
|
||||||
NimMinor* {.intdefine.}: int = 19
|
|
||||||
## The minor number of Nim's version.
|
|
||||||
|
|
||||||
NimPatch* {.intdefine.}: int = 9
|
|
||||||
## The patch number of Nim's version.
|
|
||||||
|
|
||||||
NimVersion*: string = $NimMajor & "." & $NimMinor & "." & $NimPatch
|
|
||||||
## The version of Nim as a string.
|
|
||||||
|
|
||||||
# GC interface:
|
# GC interface:
|
||||||
|
|
||||||
|
|
@ -2944,12 +2877,12 @@ proc `==`*[T](x, y: seq[T]): bool {.noSideEffect.} =
|
||||||
return true
|
return true
|
||||||
|
|
||||||
proc astToStr*[T](x: T): string {.magic: "AstToStr", noSideEffect.}
|
proc astToStr*[T](x: T): string {.magic: "AstToStr", noSideEffect.}
|
||||||
## converts the AST of `x` into a string representation. This is very useful
|
## Converts the AST of `x` into a string representation. This is very useful
|
||||||
## for debugging.
|
## for debugging.
|
||||||
|
|
||||||
proc instantiationInfo*(index = -1, fullPaths = false): tuple[
|
proc instantiationInfo*(index = -1, fullPaths = false): tuple[
|
||||||
filename: string, line: int, column: int] {.magic: "InstantiationInfo", noSideEffect.}
|
filename: string, line: int, column: int] {.magic: "InstantiationInfo", noSideEffect.}
|
||||||
## provides access to the compiler's instantiation stack line information
|
## Provides access to the compiler's instantiation stack line information
|
||||||
## of a template.
|
## of a template.
|
||||||
##
|
##
|
||||||
## While similar to the `caller info`:idx: of other languages, it is determined
|
## While similar to the `caller info`:idx: of other languages, it is determined
|
||||||
|
|
@ -2982,6 +2915,16 @@ proc instantiationInfo*(index = -1, fullPaths = false): tuple[
|
||||||
## testException(IndexError, tester(1))
|
## testException(IndexError, tester(1))
|
||||||
## # --> Test failure at example.nim:20 with 'tester(1)'
|
## # --> Test failure at example.nim:20 with 'tester(1)'
|
||||||
|
|
||||||
|
proc compiles*(x: untyped): bool {.magic: "Compiles", noSideEffect, compileTime.} =
|
||||||
|
## Special compile-time procedure that checks whether `x` can be compiled
|
||||||
|
## without any semantic error.
|
||||||
|
## This can be used to check whether a type supports some operation:
|
||||||
|
##
|
||||||
|
## .. code-block:: Nim
|
||||||
|
## when compiles(3 + 4):
|
||||||
|
## echo "'+' for integers is available"
|
||||||
|
discard
|
||||||
|
|
||||||
|
|
||||||
import system/assertions
|
import system/assertions
|
||||||
export assertions
|
export assertions
|
||||||
|
|
@ -3040,99 +2983,7 @@ proc `<`*[T: tuple](x, y: T): bool =
|
||||||
if c > 0: return false
|
if c > 0: return false
|
||||||
return false
|
return false
|
||||||
|
|
||||||
proc compiles*(x: untyped): bool {.magic: "Compiles", noSideEffect, compileTime.} =
|
|
||||||
## Special compile-time procedure that checks whether `x` can be compiled
|
|
||||||
## without any semantic error.
|
|
||||||
##
|
|
||||||
## This can be used to check whether a type supports some operation:
|
|
||||||
##
|
|
||||||
## .. code-block:: Nim
|
|
||||||
## when compiles(3 + 4):
|
|
||||||
## echo "'+' for integers is available"
|
|
||||||
discard
|
|
||||||
|
|
||||||
include "system/helpers" # for `lineInfoToString`, `isNamedTuple`
|
|
||||||
|
|
||||||
proc `$`*[T: tuple|object](x: T): string =
|
|
||||||
## Generic ``$`` operator for tuples that is lifted from the components
|
|
||||||
## of `x`. Example:
|
|
||||||
##
|
|
||||||
## .. code-block:: Nim
|
|
||||||
## $(23, 45) == "(23, 45)"
|
|
||||||
## $(a: 23, b: 45) == "(a: 23, b: 45)"
|
|
||||||
## $() == "()"
|
|
||||||
result = "("
|
|
||||||
var firstElement = true
|
|
||||||
const isNamed = T is object or isNamedTuple(T)
|
|
||||||
when not isNamed:
|
|
||||||
var count = 0
|
|
||||||
for name, value in fieldPairs(x):
|
|
||||||
if not firstElement: result.add(", ")
|
|
||||||
when isNamed:
|
|
||||||
result.add(name)
|
|
||||||
result.add(": ")
|
|
||||||
else:
|
|
||||||
count.inc
|
|
||||||
when compiles($value):
|
|
||||||
when value isnot string and value isnot seq and compiles(value.isNil):
|
|
||||||
if value.isNil: result.add "nil"
|
|
||||||
else: result.addQuoted(value)
|
|
||||||
else:
|
|
||||||
result.addQuoted(value)
|
|
||||||
firstElement = false
|
|
||||||
else:
|
|
||||||
result.add("...")
|
|
||||||
firstElement = false
|
|
||||||
when not isNamed:
|
|
||||||
if count == 1:
|
|
||||||
result.add(",") # $(1,) should print as the semantically legal (1,)
|
|
||||||
|
|
||||||
result.add(")")
|
|
||||||
|
|
||||||
proc collectionToString[T](x: T, prefix, separator, suffix: string): string =
|
|
||||||
result = prefix
|
|
||||||
var firstElement = true
|
|
||||||
for value in items(x):
|
|
||||||
if firstElement:
|
|
||||||
firstElement = false
|
|
||||||
else:
|
|
||||||
result.add(separator)
|
|
||||||
|
|
||||||
when value isnot string and value isnot seq and compiles(value.isNil):
|
|
||||||
# this branch should not be necessary
|
|
||||||
if value.isNil:
|
|
||||||
result.add "nil"
|
|
||||||
else:
|
|
||||||
result.addQuoted(value)
|
|
||||||
else:
|
|
||||||
result.addQuoted(value)
|
|
||||||
result.add(suffix)
|
|
||||||
|
|
||||||
proc `$`*[T](x: set[T]): string =
|
|
||||||
## Generic ``$`` operator for sets that is lifted from the components
|
|
||||||
## of `x`. Example:
|
|
||||||
##
|
|
||||||
## .. code-block:: Nim
|
|
||||||
## ${23, 45} == "{23, 45}"
|
|
||||||
collectionToString(x, "{", ", ", "}")
|
|
||||||
|
|
||||||
proc `$`*[T](x: seq[T]): string =
|
|
||||||
## Generic ``$`` operator for seqs that is lifted from the components
|
|
||||||
## of `x`. Example:
|
|
||||||
##
|
|
||||||
## .. code-block:: Nim
|
|
||||||
## $(@[23, 45]) == "@[23, 45]"
|
|
||||||
collectionToString(x, "@[", ", ", "]")
|
|
||||||
|
|
||||||
proc `$`*[T, U](x: HSlice[T, U]): string =
|
|
||||||
## Generic ``$`` operator for slices that is lifted from the components
|
|
||||||
## of `x`. Example:
|
|
||||||
##
|
|
||||||
## .. code-block:: Nim
|
|
||||||
## $(1 .. 5) == "1 .. 5"
|
|
||||||
result = $x.a
|
|
||||||
result.add(" .. ")
|
|
||||||
result.add($x.b)
|
|
||||||
|
|
||||||
# ----------------- GC interface ---------------------------------------------
|
# ----------------- GC interface ---------------------------------------------
|
||||||
|
|
||||||
|
|
@ -3481,6 +3332,25 @@ template unlikely*(val: bool): bool =
|
||||||
else:
|
else:
|
||||||
unlikelyProc(val)
|
unlikelyProc(val)
|
||||||
|
|
||||||
|
|
||||||
|
import system/dollars
|
||||||
|
export dollars
|
||||||
|
|
||||||
|
|
||||||
|
const
|
||||||
|
NimMajor* {.intdefine.}: int = 0
|
||||||
|
## is the major number of Nim's version.
|
||||||
|
|
||||||
|
NimMinor* {.intdefine.}: int = 19
|
||||||
|
## is the minor number of Nim's version.
|
||||||
|
|
||||||
|
NimPatch* {.intdefine.}: int = 9
|
||||||
|
## is the patch number of Nim's version.
|
||||||
|
|
||||||
|
NimVersion*: string = $NimMajor & "." & $NimMinor & "." & $NimPatch
|
||||||
|
## is the version of Nim as a string.
|
||||||
|
|
||||||
|
|
||||||
type
|
type
|
||||||
FileSeekPos* = enum ## Position relative to which seek should happen.
|
FileSeekPos* = enum ## Position relative to which seek should happen.
|
||||||
# The values are ordered so that they match with stdio
|
# The values are ordered so that they match with stdio
|
||||||
|
|
@ -3489,6 +3359,7 @@ type
|
||||||
fspCur ## Seek relative to current position
|
fspCur ## Seek relative to current position
|
||||||
fspEnd ## Seek relative to end
|
fspEnd ## Seek relative to end
|
||||||
|
|
||||||
|
|
||||||
when not defined(JS): #and not defined(nimscript):
|
when not defined(JS): #and not defined(nimscript):
|
||||||
{.push stack_trace: off, profiler:off.}
|
{.push stack_trace: off, profiler:off.}
|
||||||
|
|
||||||
|
|
@ -3799,18 +3670,6 @@ elif defined(JS):
|
||||||
if x < y: return -1
|
if x < y: return -1
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
when not defined(nimNoArrayToString):
|
|
||||||
proc `$`*[T, IDX](x: array[IDX, T]): string =
|
|
||||||
## Generic ``$`` operator for arrays that is lifted from the components.
|
|
||||||
collectionToString(x, "[", ", ", "]")
|
|
||||||
|
|
||||||
proc `$`*[T](x: openarray[T]): string =
|
|
||||||
## Generic ``$`` operator for openarrays that is lifted from the components
|
|
||||||
## of `x`. Example:
|
|
||||||
##
|
|
||||||
## .. code-block:: Nim
|
|
||||||
## $(@[23, 45].toOpenArray(0, 1)) == "[23, 45]"
|
|
||||||
collectionToString(x, "[", ", ", "]")
|
|
||||||
|
|
||||||
proc quit*(errormsg: string, errorcode = QuitFailure) {.noReturn.} =
|
proc quit*(errormsg: string, errorcode = QuitFailure) {.noReturn.} =
|
||||||
## A shorthand for ``echo(errormsg); quit(errorcode)``.
|
## A shorthand for ``echo(errormsg); quit(errorcode)``.
|
||||||
|
|
@ -4481,14 +4340,6 @@ when defined(genode):
|
||||||
# Perform application initialization
|
# Perform application initialization
|
||||||
# and return to thread entrypoint.
|
# and return to thread entrypoint.
|
||||||
|
|
||||||
proc `$`*(t: typedesc): string {.magic: "TypeTrait".} =
|
|
||||||
## Returns the name of the given type.
|
|
||||||
##
|
|
||||||
## For more procedures dealing with ``typedesc``, see ``typetraits.nim``.
|
|
||||||
runnableExamples:
|
|
||||||
doAssert $(type(42)) == "int"
|
|
||||||
doAssert $(type("Foo")) == "string"
|
|
||||||
static: doAssert $(type(@['A', 'B'])) == "seq[char]"
|
|
||||||
|
|
||||||
when defined(nimHasDefault):
|
when defined(nimHasDefault):
|
||||||
proc default*(T: typedesc): T {.magic: "Default", noSideEffect.}
|
proc default*(T: typedesc): T {.magic: "Default", noSideEffect.}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,20 @@
|
||||||
include "system/helpers"
|
|
||||||
|
|
||||||
when not declared(sysFatal):
|
when not declared(sysFatal):
|
||||||
include "system/fatal"
|
include "system/fatal"
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# helpers
|
||||||
|
|
||||||
|
type InstantiationInfo = tuple[filename: string, line: int, column: int]
|
||||||
|
|
||||||
|
proc `$`(x: int): string {.magic: "IntToStr", noSideEffect.}
|
||||||
|
|
||||||
|
proc `$`(info: InstantiationInfo): string =
|
||||||
|
# The +1 is needed here
|
||||||
|
# instead of overriding `$` (and changing its meaning), consider explicit name.
|
||||||
|
info.fileName & "(" & $info.line & ", " & $(info.column+1) & ")"
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
proc raiseAssert*(msg: string) {.noinline, noReturn.} =
|
proc raiseAssert*(msg: string) {.noinline, noReturn.} =
|
||||||
sysFatal(AssertionError, msg)
|
sysFatal(AssertionError, msg)
|
||||||
|
|
|
||||||
158
lib/system/dollars.nim
Normal file
158
lib/system/dollars.nim
Normal file
|
|
@ -0,0 +1,158 @@
|
||||||
|
proc `$`*(x: int): string {.magic: "IntToStr", noSideEffect.}
|
||||||
|
## The stringify operator for an integer argument. Returns `x`
|
||||||
|
## converted to a decimal string. ``$`` is Nim's general way of
|
||||||
|
## spelling `toString`:idx:.
|
||||||
|
|
||||||
|
proc `$`*(x: int64): string {.magic: "Int64ToStr", noSideEffect.}
|
||||||
|
## The stringify operator for an integer argument. Returns `x`
|
||||||
|
## converted to a decimal string.
|
||||||
|
|
||||||
|
proc `$`*(x: float): string {.magic: "FloatToStr", noSideEffect.}
|
||||||
|
## The stringify operator for a float argument. Returns `x`
|
||||||
|
## converted to a decimal string.
|
||||||
|
|
||||||
|
proc `$`*(x: bool): string {.magic: "BoolToStr", noSideEffect.}
|
||||||
|
## The stringify operator for a boolean argument. Returns `x`
|
||||||
|
## converted to the string "false" or "true".
|
||||||
|
|
||||||
|
proc `$`*(x: char): string {.magic: "CharToStr", noSideEffect.}
|
||||||
|
## The stringify operator for a character argument. Returns `x`
|
||||||
|
## converted to a string.
|
||||||
|
##
|
||||||
|
## .. code-block:: Nim
|
||||||
|
## assert $'c' == "c"
|
||||||
|
|
||||||
|
proc `$`*(x: cstring): string {.magic: "CStrToStr", noSideEffect.}
|
||||||
|
## The stringify operator for a CString argument. Returns `x`
|
||||||
|
## converted to a string.
|
||||||
|
|
||||||
|
proc `$`*(x: string): string {.magic: "StrToStr", noSideEffect.}
|
||||||
|
## The stringify operator for a string argument. Returns `x`
|
||||||
|
## as it is. This operator is useful for generic code, so
|
||||||
|
## that ``$expr`` also works if ``expr`` is already a string.
|
||||||
|
|
||||||
|
proc `$`*[Enum: enum](x: Enum): string {.magic: "EnumToStr", noSideEffect.}
|
||||||
|
## The stringify operator for an enumeration argument. This works for
|
||||||
|
## any enumeration type thanks to compiler magic.
|
||||||
|
##
|
||||||
|
## If a ``$`` operator for a concrete enumeration is provided, this is
|
||||||
|
## used instead. (In other words: *Overwriting* is possible.)
|
||||||
|
|
||||||
|
proc `$`*(t: typedesc): string {.magic: "TypeTrait".}
|
||||||
|
## Returns the name of the given type.
|
||||||
|
##
|
||||||
|
## For more procedures dealing with ``typedesc``, see
|
||||||
|
## `typetraits module <typetraits.html>`_.
|
||||||
|
##
|
||||||
|
## .. code-block:: Nim
|
||||||
|
## doAssert $(type(42)) == "int"
|
||||||
|
## doAssert $(type("Foo")) == "string"
|
||||||
|
## static: doAssert $(type(@['A', 'B'])) == "seq[char]"
|
||||||
|
|
||||||
|
|
||||||
|
proc isNamedTuple(T: typedesc): bool =
|
||||||
|
# Taken from typetraits.
|
||||||
|
when T isnot tuple: result = false
|
||||||
|
else:
|
||||||
|
var t: T
|
||||||
|
for name, _ in t.fieldPairs:
|
||||||
|
when name == "Field0":
|
||||||
|
return compiles(t.Field0)
|
||||||
|
else:
|
||||||
|
return true
|
||||||
|
return false
|
||||||
|
|
||||||
|
proc `$`*[T: tuple|object](x: T): string =
|
||||||
|
## Generic ``$`` operator for tuples that is lifted from the components
|
||||||
|
## of `x`. Example:
|
||||||
|
##
|
||||||
|
## .. code-block:: Nim
|
||||||
|
## $(23, 45) == "(23, 45)"
|
||||||
|
## $(a: 23, b: 45) == "(a: 23, b: 45)"
|
||||||
|
## $() == "()"
|
||||||
|
result = "("
|
||||||
|
var firstElement = true
|
||||||
|
const isNamed = T is object or isNamedTuple(T)
|
||||||
|
when not isNamed:
|
||||||
|
var count = 0
|
||||||
|
for name, value in fieldPairs(x):
|
||||||
|
if not firstElement: result.add(", ")
|
||||||
|
when isNamed:
|
||||||
|
result.add(name)
|
||||||
|
result.add(": ")
|
||||||
|
else:
|
||||||
|
count.inc
|
||||||
|
when compiles($value):
|
||||||
|
when value isnot string and value isnot seq and compiles(value.isNil):
|
||||||
|
if value.isNil: result.add "nil"
|
||||||
|
else: result.addQuoted(value)
|
||||||
|
else:
|
||||||
|
result.addQuoted(value)
|
||||||
|
firstElement = false
|
||||||
|
else:
|
||||||
|
result.add("...")
|
||||||
|
firstElement = false
|
||||||
|
when not isNamed:
|
||||||
|
if count == 1:
|
||||||
|
result.add(",") # $(1,) should print as the semantically legal (1,)
|
||||||
|
result.add(")")
|
||||||
|
|
||||||
|
|
||||||
|
proc collectionToString[T](x: T, prefix, separator, suffix: string): string =
|
||||||
|
result = prefix
|
||||||
|
var firstElement = true
|
||||||
|
for value in items(x):
|
||||||
|
if firstElement:
|
||||||
|
firstElement = false
|
||||||
|
else:
|
||||||
|
result.add(separator)
|
||||||
|
|
||||||
|
when value isnot string and value isnot seq and compiles(value.isNil):
|
||||||
|
# this branch should not be necessary
|
||||||
|
if value.isNil:
|
||||||
|
result.add "nil"
|
||||||
|
else:
|
||||||
|
result.addQuoted(value)
|
||||||
|
else:
|
||||||
|
result.addQuoted(value)
|
||||||
|
result.add(suffix)
|
||||||
|
|
||||||
|
proc `$`*[T](x: set[T]): string =
|
||||||
|
## Generic ``$`` operator for sets that is lifted from the components
|
||||||
|
## of `x`. Example:
|
||||||
|
##
|
||||||
|
## .. code-block:: Nim
|
||||||
|
## ${23, 45} == "{23, 45}"
|
||||||
|
collectionToString(x, "{", ", ", "}")
|
||||||
|
|
||||||
|
proc `$`*[T](x: seq[T]): string =
|
||||||
|
## Generic ``$`` operator for seqs that is lifted from the components
|
||||||
|
## of `x`. Example:
|
||||||
|
##
|
||||||
|
## .. code-block:: Nim
|
||||||
|
## $(@[23, 45]) == "@[23, 45]"
|
||||||
|
collectionToString(x, "@[", ", ", "]")
|
||||||
|
|
||||||
|
proc `$`*[T, U](x: HSlice[T, U]): string =
|
||||||
|
## Generic ``$`` operator for slices that is lifted from the components
|
||||||
|
## of `x`. Example:
|
||||||
|
##
|
||||||
|
## .. code-block:: Nim
|
||||||
|
## $(1 .. 5) == "1 .. 5"
|
||||||
|
result = $x.a
|
||||||
|
result.add(" .. ")
|
||||||
|
result.add($x.b)
|
||||||
|
|
||||||
|
|
||||||
|
when not defined(nimNoArrayToString):
|
||||||
|
proc `$`*[T, IDX](x: array[IDX, T]): string =
|
||||||
|
## Generic ``$`` operator for arrays that is lifted from the components.
|
||||||
|
collectionToString(x, "[", ", ", "]")
|
||||||
|
|
||||||
|
proc `$`*[T](x: openarray[T]): string =
|
||||||
|
## Generic ``$`` operator for openarrays that is lifted from the components
|
||||||
|
## of `x`. Example:
|
||||||
|
##
|
||||||
|
## .. code-block:: Nim
|
||||||
|
## $(@[23, 45].toOpenArray(0, 1)) == "[23, 45]"
|
||||||
|
collectionToString(x, "[", ", ", "]")
|
||||||
|
|
@ -1,20 +0,0 @@
|
||||||
# TODO: remove this file. It causes "declared but not used" warnings everywhere.
|
|
||||||
|
|
||||||
type InstantiationInfo = tuple[filename: string, line: int, column: int]
|
|
||||||
|
|
||||||
proc `$`(info: InstantiationInfo): string =
|
|
||||||
info.fileName & "(" & $info.line & ", " & $(info.column+1) & ")"
|
|
||||||
|
|
||||||
proc isNamedTuple(T: type): bool =
|
|
||||||
## return true for named tuples, false for any other type.
|
|
||||||
when T isnot tuple: result = false
|
|
||||||
else:
|
|
||||||
var t: T
|
|
||||||
for name, _ in t.fieldPairs:
|
|
||||||
when name == "Field0":
|
|
||||||
return compiles(t.Field0)
|
|
||||||
else:
|
|
||||||
return true
|
|
||||||
# empty tuple should be un-named,
|
|
||||||
# see https://github.com/nim-lang/Nim/issues/8861#issue-356631191
|
|
||||||
return false
|
|
||||||
|
|
@ -199,7 +199,7 @@ iterator fields*[S:tuple|object, T:tuple|object](x: S, y: T): tuple[a,b: untyped
|
||||||
magic: "Fields", noSideEffect.}
|
magic: "Fields", noSideEffect.}
|
||||||
## Iterates over every field of `x` and `y`.
|
## Iterates over every field of `x` and `y`.
|
||||||
##
|
##
|
||||||
## **Warning**: This is really transforms the 'for' and unrolls the loop.
|
## **Warning**: This really transforms the 'for' and unrolls the loop.
|
||||||
## The current implementation also has a bug that affects symbol binding
|
## The current implementation also has a bug that affects symbol binding
|
||||||
## in the loop body.
|
## in the loop body.
|
||||||
iterator fieldPairs*[T: tuple|object](x: T): RootObj {.
|
iterator fieldPairs*[T: tuple|object](x: T): RootObj {.
|
||||||
|
|
|
||||||
|
|
@ -293,7 +293,9 @@ proc nimCharToStr(x: char): string {.compilerRtl.} =
|
||||||
result = newString(1)
|
result = newString(1)
|
||||||
result[0] = x
|
result[0] = x
|
||||||
|
|
||||||
proc `$`(x: uint64): string =
|
proc `$`*(x: uint64): string {.noSideEffect.} =
|
||||||
|
## The stringify operator for an unsigned integer argument. Returns `x`
|
||||||
|
## converted to a decimal string.
|
||||||
if x == 0:
|
if x == 0:
|
||||||
result = "0"
|
result = "0"
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ const expected = """
|
||||||
tfailedassert_stacktrace.nim(34) tfailedassert_stacktrace
|
tfailedassert_stacktrace.nim(34) tfailedassert_stacktrace
|
||||||
tfailedassert_stacktrace.nim(33) foo
|
tfailedassert_stacktrace.nim(33) foo
|
||||||
assertions.nim(*) failedAssertImpl
|
assertions.nim(*) failedAssertImpl
|
||||||
assertions.nim(*) raiseAssert
|
assertions.nim(*) raiseAssert
|
||||||
fatal.nim(*) sysFatal"""
|
fatal.nim(*) sysFatal"""
|
||||||
|
|
||||||
proc tmatch(x, p: string): bool =
|
proc tmatch(x, p: string): bool =
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,5 @@
|
||||||
# todo: merge with $nimc_D/tests/metatype/ttypetraits.nim (currently disabled)
|
# todo: merge with $nimc_D/tests/metatype/ttypetraits.nim (currently disabled)
|
||||||
|
|
||||||
from typetraits import `$` # checks fix for https://github.com/c-blake/cligen/issues/84
|
|
||||||
|
|
||||||
import typetraits
|
import typetraits
|
||||||
|
|
||||||
block: # isNamedTuple
|
block: # isNamedTuple
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue