mirror of
https://github.com/nim-lang/Nim.git
synced 2026-06-14 23:53:47 +00:00
adds modifierMode parameter to typeof (#25815)
This PR adds 3 modes to `typeof` to specify how to handle type modifiers
`var`, `sink` and `lent`.
- typeOfModCompatible
Remove or keep type modifiers in the same way as old typeof. That means
keep `sink` but remove `var` and `lent`.
- typeOfModRemoveModifier
Remove type modifiers.
- typeOfModKeepModifier
Keep type modifiers.
Related to https://github.com/nim-lang/Nim/pull/25779
https://github.com/nim-lang/Nim/issues/25786
(cherry picked from commit 48621c217f)
This commit is contained in:
@@ -54,7 +54,12 @@ type
|
||||
typeOfProc, ## Prefer the interpretation that means `x` is a proc call.
|
||||
typeOfIter ## Prefer the interpretation that means `x` is an iterator call.
|
||||
|
||||
proc typeof*(x: untyped; mode = typeOfIter): typedesc {.
|
||||
TypeOfModifiers* = enum ## Modes to handle type modifiers `var`, `sink` and `lent`.
|
||||
CompatibleTypeModifiers, ## Remove or keep type modifiers in the same way as old typeof. That means keep `sink` but remove `var` and `lent`.
|
||||
RemoveTypeModifiers, ## Remove type modifiers.
|
||||
KeepTypeModifiers, ## Keep type modifiers.
|
||||
|
||||
proc typeof*(x: untyped; mode = typeOfIter; modifierMode = CompatibleTypeModifiers): typedesc {.
|
||||
magic: "TypeOf", noSideEffect, compileTime.} =
|
||||
## Builtin `typeof` operation for accessing the type of an expression.
|
||||
## Since version 0.20.0.
|
||||
@@ -76,6 +81,11 @@ proc typeof*(x: untyped; mode = typeOfIter): typedesc {.
|
||||
# since `typeOfProc` expects a typed expression and `myFoo2()` can
|
||||
# only be used in a `for` context.
|
||||
|
||||
proc varParam(x: var int;
|
||||
y: typeof(x, modifierMode = RemoveTypeModifiers);
|
||||
z: typeof(x, modifierMode = KeepTypeModifiers)) = discard
|
||||
doAssert varParam is proc (x: var int; y: int; z: var int) {.nimcall.}
|
||||
|
||||
proc `or`*(a, b: typedesc): typedesc {.magic: "TypeTrait", noSideEffect.}
|
||||
## Constructs an `or` meta class.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user