fix of procs + runnableExamples (#16932)
This commit is contained in:
parent
6dc7ece36a
commit
3eebbb234d
1 changed files with 25 additions and 8 deletions
|
|
@ -904,14 +904,31 @@ template disarm*(x: typed) =
|
||||||
## experimental API!
|
## experimental API!
|
||||||
x = nil
|
x = nil
|
||||||
|
|
||||||
proc `of`*[T, S](x: typedesc[T], y: typedesc[S]): bool {.magic: "Of", noSideEffect.}
|
proc `of`*[T, S](x: T, y: typedesc[S]): bool {.magic: "Of", noSideEffect.} =
|
||||||
proc `of`*[T, S](x: T, y: typedesc[S]): bool {.magic: "Of", noSideEffect.}
|
## Checks if `x` is an instance of `y`.
|
||||||
proc `of`*[T, S](x: T, y: S): bool {.magic: "Of", noSideEffect.}
|
runnableExamples:
|
||||||
## Checks if `x` has a type of `y`.
|
type
|
||||||
##
|
Base = ref object of RootObj
|
||||||
## .. code-block:: Nim
|
Sub1 = ref object of Base
|
||||||
## assert(FloatingPointDefect of Exception)
|
Sub2 = ref object of Base
|
||||||
## assert(DivByZeroDefect of Exception)
|
Unrelated = ref object
|
||||||
|
|
||||||
|
var base: Base = Sub1() # downcast
|
||||||
|
doAssert base of Base # generates `CondTrue` (statically true)
|
||||||
|
doAssert base of Sub1
|
||||||
|
doAssert base isnot Sub1
|
||||||
|
doAssert not (base of Sub2)
|
||||||
|
|
||||||
|
base = Sub2() # re-assign
|
||||||
|
doAssert base of Sub2
|
||||||
|
doAssert Sub2(base) != nil # upcast
|
||||||
|
doAssertRaises(ObjectConversionDefect): discard Sub1(base)
|
||||||
|
|
||||||
|
var sub1 = Sub1()
|
||||||
|
doAssert sub1 of Base
|
||||||
|
doAssert sub1.Base of Sub1
|
||||||
|
|
||||||
|
doAssert not compiles(base of Unrelated)
|
||||||
|
|
||||||
proc cmp*[T](x, y: T): int =
|
proc cmp*[T](x, y: T): int =
|
||||||
## Generic compare proc.
|
## Generic compare proc.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue