Difference between revisions of "As"
From Lazarus wiki
Jump to navigationJump to search (rewrite) |
|||
Line 1: | Line 1: | ||
{{as}} | {{as}} | ||
+ | The [[Operator|operator]] <syntaxhighlight lang="pascal" enclose="none">as</syntaxhighlight> performs a conditional [[Typecast|typecast]]. | ||
+ | The word <syntaxhighlight lang="pascal" enclose="none">as</syntaxhighlight> is a [[Reserved word|reserved word]] in [[Mode Delphi|<syntaxhighlight lang="pascal" enclose="none">{$mode Delphi}</syntaxhighlight>]] and [[Mode ObjFPC|<syntaxhighlight lang="pascal" enclose="none">{$mode objFPC}</syntaxhighlight>]]. | ||
− | + | == operation == | |
+ | <syntaxhighlight lang="pascal" enclose="none">as</syntaxhighlight> requires a [[Class|<syntaxhighlight lang="pascal" enclose="none">class</syntaxhighlight>]] or (COM) interface as the first argument and a class or interface reference as the second. | ||
+ | The expression <syntaxhighlight lang="pascal" enclose="none">child as super</syntaxhighlight> is equivalent to the expression and statement: | ||
+ | <syntaxhighlight lang="pascal"> | ||
+ | super(child) | ||
+ | if not assigned(child) and_then not child is super then | ||
+ | begin | ||
+ | raise exception.create(sErrInvalidTypecast); | ||
+ | end; | ||
+ | </syntaxhighlight> | ||
+ | {{Warning|Typecasting a [[Nil|<syntaxhighlight lang="pascal" enclose="none">nil</syntaxhighlight> pointer]] will not raise an exception.}} | ||
+ | However, trying to de-reference <syntaxhighlight lang="pascal" enclose="none">nil</syntaxhighlight> by attempting to access an attribute or method ''will'' cause a [[runtime error|RTE]]. | ||
− | <syntaxhighlight> | + | == application == |
− | + | <syntaxhighlight lang="pascal" enclose="none">as</syntaxhighlight> ensures a typecast is legit. | |
− | |||
− | |||
− | |||
− | </syntaxhighlight> | ||
− | + | <syntaxhighlight lang="pascal" enclose="none">as</syntaxhighlight> is one of the operators that can not be [[Operator overloading|overloaded]]. | |
− | <syntaxhighlight lang="pascal" enclose="none"> | + | == see also == |
+ | * [[Is|<syntaxhighlight lang="pascal" enclose="none">is</syntaxhighlight>]] |
Revision as of 11:27, 23 August 2019
│
Deutsch (de) │
English (en) │
español (es) │
suomi (fi) │
français (fr) │
The operator as
performs a conditional typecast.
The word as
is a reserved word in {$mode Delphi}
and {$mode objFPC}
.
operation
as
requires a class
or (COM) interface as the first argument and a class or interface reference as the second.
The expression child as super
is equivalent to the expression and statement:
super(child)
if not assigned(child) and_then not child is super then
begin
raise exception.create(sErrInvalidTypecast);
end;
Warning: Typecasting a nil
pointer will not raise an exception.
However, trying to de-reference nil
by attempting to access an attribute or method will cause a RTE.
application
as
ensures a typecast is legit.
as
is one of the operators that can not be overloaded.