diff options
Diffstat (limited to 'lib/system.nim')
-rw-r--r-- | lib/system.nim | 72 |
1 files changed, 40 insertions, 32 deletions
diff --git a/lib/system.nim b/lib/system.nim index f852a0aed..2c2783dfb 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -203,23 +203,6 @@ proc `or`*(x, y: bool): bool {.magic: "Or", noSideEffect.} proc `xor`*(x, y: bool): bool {.magic: "Xor", noSideEffect.} ## Boolean `exclusive or`; returns true iff ``x != y``. -proc new*[T](a: var ref T) {.magic: "New", noSideEffect.} - ## creates a new object of type ``T`` and returns a safe (traced) - ## reference to it in ``a``. - -proc new*(T: typedesc): auto = - ## creates a new object of type ``T`` and returns a safe (traced) - ## reference to it as result value. - ## - ## When ``T`` is a ref type then the resulting type will be ``T``, - ## otherwise it will be ``ref T``. - when (T is ref): - var r: T - else: - var r: ref T - new(r) - return r - const ThisIsSystem = true proc internalNew*[T](a: var ref T) {.magic: "New", noSideEffect.} @@ -1001,10 +984,13 @@ proc `div`*(x, y: int32): int32 {.magic: "DivI", noSideEffect.} ## ``trunc(x/y)``. ## ## .. code-block:: Nim - ## 1 div 2 == 0 - ## 2 div 2 == 1 - ## 3 div 2 == 1 - ## 7 div 5 == 1 + ## ( 1 div 2) == 0 + ## ( 2 div 2) == 1 + ## ( 3 div 2) == 1 + ## ( 7 div 3) == 2 + ## (-7 div 3) == -2 + ## ( 7 div -3) == -2 + ## (-7 div -3) == 2 when defined(nimnomagic64): proc `div`*(x, y: int64): int64 {.magic: "DivI", noSideEffect.} @@ -1020,7 +1006,10 @@ proc `mod`*(x, y: int32): int32 {.magic: "ModI", noSideEffect.} ## ``x - (x div y) * y``. ## ## .. code-block:: Nim - ## (7 mod 5) == 2 + ## ( 7 mod 5) == 2 + ## (-7 mod 5) == -2 + ## ( 7 mod -5) == 2 + ## (-7 mod -5) == -2 when defined(nimnomagic64): proc `mod`*(x, y: int64): int64 {.magic: "ModI", noSideEffect.} @@ -1325,6 +1314,23 @@ proc `is`*[T, S](x: T, y: S): bool {.magic: "Is", noSideEffect.} template `isnot`*(x, y: untyped): untyped = not (x is y) ## Negated version of `is`. Equivalent to ``not(x is y)``. +proc new*[T](a: var ref T) {.magic: "New", noSideEffect.} + ## creates a new object of type ``T`` and returns a safe (traced) + ## reference to it in ``a``. + +proc new*(T: typedesc): auto = + ## creates a new object of type ``T`` and returns a safe (traced) + ## reference to it as result value. + ## + ## When ``T`` is a ref type then the resulting type will be ``T``, + ## otherwise it will be ``ref T``. + when (T is ref): + var r: T + else: + var r: ref T + new(r) + return r + 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: S): bool {.magic: "Of", noSideEffect.} @@ -2084,6 +2090,8 @@ when not defined(nimscript) and hasAlloc: ## returns the number of bytes on the shared heap that are owned by the ## process. This is only available when threads are enabled. +proc `|`*(a, b: typedesc): typedesc = discard + when sizeof(int) <= 2: type IntLikeForCount = int|int8|int16|char|bool|uint8|enum else: @@ -2634,6 +2642,16 @@ proc `<`*[T: tuple](x, y: T): bool = if c > 0: return false return false +proc compiles*(x: untyped): bool {.magic: "Compiles", noSideEffect, compileTime.} = + ## Special compile-time procedure that checks whether `x` can be compiled + ## without any semantic error. + ## This can be used to check whether a type supports some operation: + ## + ## .. code-block:: Nim + ## when compiles(3 + 4): + ## echo "'+' for integers is available" + discard + proc `$`*[T: tuple|object](x: T): string = ## generic ``$`` operator for tuples that is lifted from the components ## of `x`. Example: @@ -4045,16 +4063,6 @@ when hasAlloc: x[j+i] = item[j] inc(j) -proc compiles*(x: untyped): bool {.magic: "Compiles", noSideEffect, compileTime.} = - ## Special compile-time procedure that checks whether `x` can be compiled - ## without any semantic error. - ## This can be used to check whether a type supports some operation: - ## - ## .. code-block:: Nim - ## when compiles(3 + 4): - ## echo "'+' for integers is available" - discard - when declared(initDebugger): initDebugger() |