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
parent ad5063aed1
commit 128d21be1c
2 changed files with 16 additions and 9 deletions

View File

@@ -113,8 +113,12 @@
- Make `{.requiresInit.}` pragma to work for `distinct` types.
- Added a macros `enumLen` for returning the number of items in an enum to the
`typetraits.nim` module.
- `typetraits`:
`distinctBase` now is identity instead of error for non distinct types.
Added `enumLen` to return the number of elements in an enum.
Added `HoleyEnum` for enums with holes, `OrdinalEnum` for enums without holes.
Added `hasClosure`.
Added `pointerBase` to return `T` for `ref T | ptr T`.
- `prelude` now works with the JavaScript target.
Added `sequtils` import to `prelude`.
@@ -162,8 +166,6 @@
Added `symbolName` to return the enum symbol name ignoring the human readable name.
Added `symbolRank` to return the index in which an enum member is listed in an enum.
- Added `typetraits.HoleyEnum` for enums with holes, `OrdinalEnum` for enums without holes.
- Removed deprecated `iup` module from stdlib, it has already moved to
[nimble](https://github.com/nim-lang/iup).
@@ -293,7 +295,6 @@
- Added `algorithm.merge`.
- Added `std/jsfetch` module [Fetch](https://developer.mozilla.org/docs/Web/API/Fetch_API) wrapper for JavaScript target.
- Added `std/jsheaders` module [Headers](https://developer.mozilla.org/en-US/docs/Web/API/Headers) wrapper for JavaScript target.
@@ -322,8 +323,6 @@
- Added `hasDataBuffered` to `asyncnet`.
- Added `hasClosure` to `std/typetraits`.
- Added `std/tempfiles`.
- Added `genasts.genAst` that avoids the problems inherent with `quote do` and can
@@ -345,8 +344,6 @@
- nil dereference is not allowed at compile time. `cast[ptr int](nil)[]` is rejected at compile time.
- `typetraits.distinctBase` now is identity instead of error for non distinct types.
- `os.copyFile` is now 2.5x faster on OSX, by using `copyfile` from `copyfile.h`;
use `-d:nimLegacyCopyFile` for OSX < 10.5.

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.
##