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" | + | The [[Operator|operator]] <syntaxhighlight lang="pascal" inline>as</syntaxhighlight> performs a conditional [[Typecast|typecast]]. |
− | The word <syntaxhighlight lang="pascal" | + | The word <syntaxhighlight lang="pascal" inline>as</syntaxhighlight> is a [[Reserved word|reserved word]] in [[Mode Delphi|<syntaxhighlight lang="pascal" inline>{$mode Delphi}</syntaxhighlight>]] and [[Mode ObjFPC|<syntaxhighlight lang="pascal" inline>{$mode objFPC}</syntaxhighlight>]]. |
== operation == | == operation == | ||
− | <syntaxhighlight lang="pascal" | + | <syntaxhighlight lang="pascal" inline>as</syntaxhighlight> requires a [[Class|<syntaxhighlight lang="pascal" inline>class</syntaxhighlight>]] or (COM) interface as the first argument and a class or interface reference as the second. |
− | The expression <syntaxhighlight lang="pascal" | + | The expression <syntaxhighlight lang="pascal" inline>child as super</syntaxhighlight> is equivalent to the expression and statement: |
<syntaxhighlight lang="pascal"> | <syntaxhighlight lang="pascal"> | ||
super(child) | super(child) | ||
Line 14: | Line 14: | ||
end; | end; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | {{Warning|Typecasting a [[Nil|<syntaxhighlight lang="pascal" | + | {{Warning|Typecasting a [[Nil|<syntaxhighlight lang="pascal" inline>nil</syntaxhighlight> pointer]] will not raise an exception.}} |
− | However, trying to de-reference <syntaxhighlight lang="pascal" | + | However, trying to de-reference <syntaxhighlight lang="pascal" inline>nil</syntaxhighlight> by attempting to access an attribute or method ''will'' cause a [[runtime error|RTE]]. |
== application == | == application == | ||
− | <syntaxhighlight lang="pascal" | + | <syntaxhighlight lang="pascal" inline>as</syntaxhighlight> ensures a typecast is legit. |
− | <syntaxhighlight lang="pascal" | + | <syntaxhighlight lang="pascal" inline>as</syntaxhighlight> is one of the operators that can not be [[Operator overloading|overloaded]]. |
== see also == | == see also == | ||
− | * [[Is|<syntaxhighlight lang="pascal" | + | * [[Is|<syntaxhighlight lang="pascal" inline>is</syntaxhighlight>]] |
Latest revision as of 17:23, 6 August 2022
│
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.