Skip to content

Commit

Permalink
add typetraits.pointerBase to return T in ref T|ptr T (nim-lang…
Browse files Browse the repository at this point in the history
…#18293)

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

* deref => refBase

* refBase=>pointerBase

* [skip ci] address comment
  • Loading branch information
timotheecour authored and PMunch committed Mar 28, 2022
1 parent a66fa59 commit 45dfe41
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
15 changes: 6 additions & 9 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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).

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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.

Expand Down
10 changes: 10 additions & 0 deletions lib/pure/typetraits.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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.
##
Expand Down

0 comments on commit 45dfe41

Please sign in to comment.