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 [[Operator|operator]] <syntaxhighlight lang="pascal" inline>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>]].
+
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" 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.
+
<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" enclose="none">child as super</syntaxhighlight> is equivalent to the expression and statement:
+
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" enclose="none">nil</syntaxhighlight> pointer]] will not raise an exception.}}
+
{{Warning|Typecasting a [[Nil|<syntaxhighlight lang="pascal" inline>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]].
+
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" enclose="none">as</syntaxhighlight> ensures a typecast is legit.
+
<syntaxhighlight lang="pascal" inline>as</syntaxhighlight> ensures a typecast is legit.
  
<syntaxhighlight lang="pascal" enclose="none">as</syntaxhighlight> is one of the operators that can not be [[Operator overloading|overloaded]].
+
<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" enclose="none">is</syntaxhighlight>]]
+
* [[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-icon.png

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.

see also