add typetraits.pointerBase to return T in ref T|ptr T (#18293)

* add typetraits.deref to return T in ref T|ptr T

* deref => refBase

* refBase=>pointerBase

* [skip ci] address comment
This commit is contained in:
Timothee Cour 2021-06-20 00:49:18 -07:00 • committed by GitHub
commit 128d21be1c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 9 deletions

View file

@ -100,6 +100,16 @@ proc isNamedTuple*(T: typedesc): bool {.magic: "TypeTrait".} =
doAssert not isNamedTuple((string, int))
doAssert isNamedTuple(tuple[name: string, age: int])
template pointerBase*[T](_: typedesc[ptr T | ref T]): typedesc =
## Returns `T` for `ref T | ptr T`.
runnableExamples:
assert (ref int).pointerBase is int
type A = ptr seq[float]
assert A.pointerBase is seq[float]
assert (ref A).pointerBase is A # not seq[float]
assert (var s = "abc"; s[0].addr).typeof.pointerBase is char
T
proc distinctBase*(T: typedesc): typedesc {.magic: "TypeTrait".} =
## Returns the base type for distinct types, or the type itself otherwise.
##