mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-31 18:32:11 +00:00
* add `isNamedTuple`; make $(1, 2) be (1, 2) instead of leaking implementation detail (Field0: 1, Field1: 2) fixes this: #8670 (comment) /cc @alehander42 @Vindaar @mratsim * Note: isNamedTuple is useful in other places, eg #10010 (comment) * move isNamedTuple to helpers.nim to avoid exposing new symbol to system.nim * remove workaround in tests/vm/tissues.nim failing test now that #10218 was makes it work
29 lines
1002 B
Nim
29 lines
1002 B
Nim
# helpers used system.nim and other modules, avoids code duplication while
|
|
# also minimizing symbols exposed in system.nim
|
|
#
|
|
# TODO: move other things here that should not be exposed in system.nim
|
|
|
|
proc lineInfoToString(file: string, line, column: int): string =
|
|
file & "(" & $line & ", " & $column & ")"
|
|
|
|
type InstantiationInfo = tuple[filename: string, line: int, column: int]
|
|
|
|
proc `$`(info: InstantiationInfo): string =
|
|
# The +1 is needed here
|
|
# instead of overriding `$` (and changing its meaning), consider explicit name.
|
|
lineInfoToString(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
|