From b8dc58d8843af7dbd0ee2f3274e0d4cf18391dd2 Mon Sep 17 00:00:00 2001 From: metagn Date: Wed, 24 Aug 2022 10:44:16 +0300 Subject: test removing dollar for objects out of system (#20242) * test removing dollar for objects out of system * test & fixes * fix bootstrap * use nimPreviewSlimSystem, test stdlib category * fix test --- lib/std/objectdollar.nim | 11 +++++++++++ lib/std/private/miscdollars.nim | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 lib/std/objectdollar.nim (limited to 'lib/std') diff --git a/lib/std/objectdollar.nim b/lib/std/objectdollar.nim new file mode 100644 index 000000000..f413bbc46 --- /dev/null +++ b/lib/std/objectdollar.nim @@ -0,0 +1,11 @@ +import std/private/miscdollars + +proc `$`*[T: object](x: T): string = + ## Generic `$` operator for objects with similar output to + ## `$` for named tuples. + runnableExamples: + type Foo = object + a, b: int + let x = Foo(a: 23, b: 45) + assert $x == "(a: 23, b: 45)" + tupleObjectDollar(result, x) diff --git a/lib/std/private/miscdollars.nim b/lib/std/private/miscdollars.nim index 840fedf54..47b788ee9 100644 --- a/lib/std/private/miscdollars.nim +++ b/lib/std/private/miscdollars.nim @@ -12,3 +12,42 @@ template toLocation*(result: var string, file: string | cstring, line: int, col: result.add ", " addInt(result, col) result.add ")" + +when defined(nimHasIsNamedTuple): + proc isNamedTuple(T: typedesc): bool {.magic: "TypeTrait".} +else: + # for bootstrap; remove after release 1.2 + 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 + +template tupleObjectDollar*[T: tuple | object](result: var string, x: T) = + result = "(" + const isNamed = T is object or isNamedTuple(typeof(T)) + var count {.used.} = 0 + for name, value in fieldPairs(x): + if count > 0: result.add(", ") + when isNamed: + result.add(name) + result.add(": ") + 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) + else: + result.add("...") + when not isNamed: + if count == 1: + result.add(",") # $(1,) should print as the semantically legal (1,) + result.add(")") -- cgit 1.4.1-2-gfad0 ng/Nim/tree/tests?h=devel&id=84534ce4b8d2717f672e870921cab46dc2397c28'>tests/threads/trecursive_actor.nim
blob: e2774704c4d37a175c0063d94ada64fd5cde7733 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
discard """
  outputsub: "0"
"""

import actors

var
  a: TActorPool[int, void]
createActorPool(a)

proc task(i: int) {.thread.} =
  echo i
  if i != 0: a.spawn (i-1, task)

# count from 9 till 0 and check 0 is somewhere in the output
a.spawn(9, task)
a.join()