diff options
author | flaviut <tamasflaviu@gmail.com> | 2014-04-08 17:44:53 -0400 |
---|---|---|
committer | flaviut <tamasflaviu@gmail.com> | 2014-04-08 17:44:53 -0400 |
commit | fb547ef76b58f16a77c149acb227f746f20edb86 (patch) | |
tree | cb0eecb78da5273c96a4e173fd648186886bff2c /lib | |
parent | af4c0851c1981e00d49742ed40eb32cfcd8f3cb0 (diff) | |
download | Nim-fb547ef76b58f16a77c149acb227f746f20edb86.tar.gz |
Code docs for `contains`, `in`, `is`, and `of`
Diffstat (limited to 'lib')
-rw-r--r-- | lib/system.nim | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/lib/system.nim b/lib/system.nim index 441f81f6f..8ce138700 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -749,15 +749,49 @@ proc contains*[T](x: set[T], y: T): bool {.magic: "InSet", noSideEffect.} ## passes its arguments in reverse order. proc contains*[T](s: TSlice[T], value: T): bool {.noSideEffect, inline.} = + ## Checks if `value` is withing the range of `s`; returns true iff + ## `value >= s.a and value <= s.b` + ## + ## .. code-block:: Nimrod + ## assert((1..3).contains(1) == true) + ## assert((1..3).contains(2) == true) + ## assert((1..3).contains(4) == false) result = s.a <= value and value <= s.b template `in` * (x, y: expr): expr {.immediate.} = contains(y, x) + ## Suger for contains + ## + ## .. code-block:: Nimrod + ## assert(1 in (1..3) == true) + ## assert(5 in (1..3) == false) template `notin` * (x, y: expr): expr {.immediate.} = not contains(y, x) + ## Sugar for not containing + ## + ## .. code-block:: Nimrod + ## assert(1 notin (1..3) == false) + ## assert(5 notin (1..3) == true) proc `is` *[T, S](x: T, y: S): bool {.magic: "Is", noSideEffect.} + ## Checks if T is of the same type as S + ## + ## .. code-block:: Nimrod + ## proc test[T](a: T): int = + ## when (T is int): + ## return a + ## else: + ## return 0 + ## + ## assert(test[int](3) == 3) + ## assert(test[string]("xyz") == 0) template `isnot` *(x, y: expr): expr {.immediate.} = not (x is y) - + ## Negated version of `is`. Equivalent to `not(is(x,y))` proc `of` *[T, S](x: T, y: S): bool {.magic: "Of", noSideEffect.} + ## Checks if `x` has a type of `y` + ## + ## .. code-block:: Nimrod + ## assert(EFloatingPoint is EBase) + ## assert(EIO is ESystem) + ## assert(EDivByZero is EBase) proc cmp*[T](x, y: T): int {.procvar.} = ## Generic compare proc. Returns a value < 0 iff x < y, a value > 0 iff x > y |