Recent Changes - Search:

Oktatás

* Programozás 1
  + feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

Teaching

Programming 1 (BI)
  ▸ exercises
  ▸ quick link

teaching assets


Félévek

* 2025/26/2
* archívum


Linkek

* kalendárium
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C#
* D lang.
* Java
* Nim
* Nim2
  + exercises
* XC=BASIC
* old
  ✦C++, ✦Clojure, ✦Scala


[ edit | logout ]
[ sandbox | passwd ]

assert

(1) assert
import std.stdio;

int add(const int a, const int b) pure
{
    assert(a > 0);
    // assert(a > 0, "`a` must be positive");
    return a + b;
}

void main()
{
    writeln(add(-2, 3));
}

Compile and run:

$ dmd main.d

$ ./main
core.exception.AssertError@main.d(7): Assertion failure
...

Disable assertions:

$ dmd main.d -release

$ ./main
1

Notes:

  • In release mode, assertions are ignored. The compiler simply drops those lines.
  • However, there is one exception. assert(0) (and assert(false), they're equivalent) is NOT ignored in release mode! Why? Because it indicates a part of the program that execution should never reach. If execution reaches that part, then it's a problem that mustn't be ignored.
  • Assertions shouldn't have any side effects, so they can be used in pure functions.
Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2025 September 06, 18:12