diff options
author | Timothee Cour <timothee.cour2@gmail.com> | 2019-01-08 18:37:25 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-01-08 18:37:25 -0800 |
commit | 6ce3949c8aa489d268f272829d03edab175bfc7a (patch) | |
tree | 777513c9aef3896204b8dfd9cea40517d57d2ce7 /lib/system | |
parent | bf3a308e86e7c5999855546962aed564218a8121 (diff) | |
download | Nim-6ce3949c8aa489d268f272829d03edab175bfc7a.tar.gz |
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
Diffstat (limited to 'lib/system')
-rw-r--r-- | lib/system/helpers.nim | 19 | ||||
-rw-r--r-- | lib/system/helpers2.nim | 2 |
2 files changed, 20 insertions, 1 deletions
diff --git a/lib/system/helpers.nim b/lib/system/helpers.nim index 7b2b32679..a7e47915e 100644 --- a/lib/system/helpers.nim +++ b/lib/system/helpers.nim @@ -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 diff --git a/lib/system/helpers2.nim b/lib/system/helpers2.nim index 1c9e7c068..c67a2c278 100644 --- a/lib/system/helpers2.nim +++ b/lib/system/helpers2.nim @@ -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 & ") " |