diff options
author | Araq <rumpf_a@web.de> | 2013-03-16 12:16:33 -0700 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2013-03-16 12:16:33 -0700 |
commit | 7304ca061159db8345ae245309274310bbfd0ab1 (patch) | |
tree | edcf3b03632e60ef0d18d35446ebf12d975b9dac /lib | |
parent | f24f9fec1db133d5ce24815cbf9c36907229720b (diff) | |
parent | d74d667e48f01f49434f86a567cfbabc4f260af9 (diff) | |
download | Nim-7304ca061159db8345ae245309274310bbfd0ab1.tar.gz |
Merge pull request #360 from gradha/pr_adds_example_to_instantiationinfo_docstring
Adds example to instantiationInfo docstring.
Diffstat (limited to 'lib')
-rwxr-xr-x | lib/system.nim | 31 |
1 files changed, 28 insertions, 3 deletions
diff --git a/lib/system.nim b/lib/system.nim index 8885de624..5bcd7b02d 100755 --- a/lib/system.nim +++ b/lib/system.nim @@ -2396,9 +2396,34 @@ proc astToStr*[T](x: T): string {.magic: "AstToStr", noSideEffect.} proc InstantiationInfo*(index = -1): tuple[filename: string, line: int] {. magic: "InstantiationInfo", noSideEffect.} ## provides access to the compiler's instantiation stack line information. - ## This is only useful for advanced meta programming. See the implementation - ## of `assert` for an example. - + ## + ## This proc is mostly useful for meta programming (eg. ``assert`` template) + ## to retrieve information about the current filename and line number. + ## Example: + ## + ## .. code-block:: nimrod + ## import strutils + ## + ## template testException(exception, code: expr): stmt = + ## try: + ## let pos = instantiationInfo() + ## discard(code) + ## echo "Test failure at $1:$2 with '$3'" % [pos.filename, + ## $pos.line, astToStr(code)] + ## assert false, "A test expecting failure succeeded?" + ## except exception: + ## nil + ## + ## proc tester(pos: int): int = + ## let + ## a = @[1, 2, 3] + ## result = a[pos] + ## + ## when isMainModule: + ## testException(EInvalidIndex, tester(30)) + ## testException(EInvalidIndex, tester(1)) + ## # --> Test failure at example.nim:20 with 'tester(1)' + proc raiseAssert*(msg: string) {.noinline.} = raise newException(EAssertionFailed, msg) |