IfThen (Assigned (Widget), Widget.Description, "No Widget") is not crashing. Should it?

In the code that I support, I found some code examples that look like this:

Description := IfThen(Assigned(Widget), Widget.Description, 'No Widget');

I expected this to work when the widget is zero, but when I tested it, it worked great.

If I recompile it with the code control disabled in Project - Options - Compiler, I get an access violation.

It seems that since IfThen is marked as inline, the compiler usually does not evaluate Widget.Description if Widget is zero.

Is there a reason why the code should be “fixed” because it does not seem to be broken? They do not want the code to be changed unnecessarily. Could this bite them?

I tested it with Delphi XE2 and XE6.

+4
source share
3 answers

Personally, I hate relying on behavior that is not contractual.

The built-in directive is a compiler suggestion.

If I understand correctly what I read, your code will also be broken if you create using runtime packages.

nesting never occurs across package boundaries

As Uli Gerhardt commented, this can be considered a mistake, in which she works in the first place. Since behavior is not contractual, it can change at any time.

- , "" . , , , , . ( , , ?)

+3

, "", , , ?

, . , , . :

  • . , DLL .
  • , , , , . , , , .

2, :

Description := IfThen(Assigned(Widget), Widget.Description, 'No Widget');

, Widget.Description , , , . .

, Widget.Description - , , . , Widget.Description .

, , :

  • , .
  • getter Description getter getter.

, . , , .

, XE7. . , . XE7 .

+4

It has already been fixed - in XE7 and confirmed that this should have been a misbehavior.

See https://quality.embarcadero.com/browse/RSP-11531

+1
source

Source: https://habr.com/ru/post/1685679/


All Articles