mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-20 06:20:38 +00:00
add isNamedTuple; make $(1, 2) be (1, 2) instead of (Field0: 1, Field1: 2) which leaked implementation detail (#10070)
* 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
This commit is contained in:
@@ -6,6 +6,23 @@
|
||||
proc lineInfoToString(file: string, line, column: int): string =
|
||||
file & "(" & $line & ", " & $column & ")"
|
||||
|
||||
proc `$`(info: type(instantiationInfo(0))): string =
|
||||
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
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
# imported by other modules, unlike helpers.nim which is included
|
||||
|
||||
template formatErrorIndexBound*[T](i, a, b: T): string =
|
||||
"index out of bounds: (a:" & $a & ") <= (i:" & $i & ") <= (b:" & $b & ") "
|
||||
|
||||
|
||||
Reference in New Issue
Block a user