Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not Nil shortcut in Delphi

Tags:

delphi

What delphi function asserts that an object is not nil?

like image 469
Peter Turner Avatar asked Sep 29 '08 15:09

Peter Turner


1 Answers

Like knight_killer pointed out above, you use the Assert() function, asserting that Assigned(obj) is true. Of course, like in most compiled languages, assertions are not executed (or even included in the compiler output) unless you've specifically enabled them, so you should not rely on assertions for release mode builds.

You can, of course, simply check against nil, a la Assert(obj <> nil). However, Assigned() produces the exact same compiler output and has the added benefit that it works on pointers to class methods too (which are in reality a pair of pointers, one to the method, and the other one to the class instance), so using Assigned() is a good habit to pick up.

like image 124
Mihai Limbășan Avatar answered Oct 17 '22 01:10

Mihai Limbășan