diff options
author | cooldome <cdome@bk.ru> | 2020-01-07 23:36:57 +0000 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2020-01-08 00:36:57 +0100 |
commit | 871d5e79b1dc54c1657a1d7f85788e8bd8f4fb28 (patch) | |
tree | 41353387591564db35c1deb7a9619fcf3a3b80d5 /lib/pure | |
parent | 8bcc7e8b9ea39524bac14f6c89de5213509f4099 (diff) | |
download | Nim-871d5e79b1dc54c1657a1d7f85788e8bd8f4fb28.tar.gz |
distinctBase type trait for distinct types (#13031)
Diffstat (limited to 'lib/pure')
-rw-r--r-- | lib/pure/sugar.nim | 23 | ||||
-rw-r--r-- | lib/pure/typetraits.nim | 17 |
2 files changed, 8 insertions, 32 deletions
diff --git a/lib/pure/sugar.nim b/lib/pure/sugar.nim index a084d4e00..e7ef63309 100644 --- a/lib/pure/sugar.nim +++ b/lib/pure/sugar.nim @@ -13,6 +13,7 @@ include system/inclrtl import macros +import typetraits proc createProcType(p, b: NimNode): NimNode {.compileTime.} = #echo treeRepr(p) @@ -160,27 +161,9 @@ proc freshIdentNodes(ast: NimNode): NimNode = result.add inspect(child) result = inspect(ast) -macro distinctBase*(T: typedesc): untyped = +template distinctBase*(T: typedesc): typedesc {.deprecated: "use distinctBase from typetraits instead".} = ## reverses ``type T = distinct A``; works recursively. - runnableExamples: - type T = distinct int - doAssert distinctBase(T) is int - doAssert: not compiles(distinctBase(int)) - type T2 = distinct T - doAssert distinctBase(T2) is int - - let typeNode = getTypeImpl(T) - expectKind(typeNode, nnkBracketExpr) - if typeNode[0].typeKind != ntyTypeDesc: - error "expected typeDesc, got " & $typeNode[0] - var typeSym = typeNode[1] - typeSym = getTypeImpl(typeSym) - if typeSym.typeKind != ntyDistinct: - error "type is not distinct" - typeSym = typeSym[0] - while typeSym.typeKind == ntyDistinct: - typeSym = getTypeImpl(typeSym)[0] - typeSym.freshIdentNodes + typetraits.distinctBase(T) macro capture*(locals: openArray[typed], body: untyped): untyped {.since: (1, 1).} = ## Useful when creating a closure in a loop to capture some local loop variables diff --git a/lib/pure/typetraits.nim b/lib/pure/typetraits.nim index 24aed52b0..7a493cb6c 100644 --- a/lib/pure/typetraits.nim +++ b/lib/pure/typetraits.nim @@ -63,19 +63,12 @@ proc supportsCopyMem*(t: typedesc): bool {.magic: "TypeTrait".} ## ## Other languages name a type like these `blob`:idx:. -proc isNamedTuple*(T: typedesc): bool = +proc isNamedTuple*(T: typedesc): bool {.magic: "TypeTrait".} ## 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 + +proc distinctBase*(T: typedesc): typedesc {.magic: "TypeTrait".} + ## Returns base type for distinct types, works only for distinct types. + ## compile time error otherwise when isMainModule: |