As: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: As
| Old | New | Line | |
|---|---|---|---|
| 1 | + | The [[Operator|operator]] `as` performs a conditional [[Typecast|typecast]]. The word `as` is a [[Reserved word|reserved word]] in [[Mode Delphi|{$mode Delphi}]] and [[Mode ObjFPC|{$mode objFPC}]]. | |
| 2 | + | ||
| 3 | + | ## operation | |
| 4 | + | ||
| 5 | + | `as` requires a [[Class|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: | |
| 6 | + | ||
| 7 | + | ``` pascal | |
| 8 | + | super(child) | |
| 9 | + | if not assigned(child) and_then not child is super then | |
| 10 | + | begin | |
| 11 | + | raise exception.create(sErrInvalidTypecast); | |
| 12 | + | end; | |
| 13 | + | ``` | |
| 14 | + | ||
| 15 | + | > **Warning:** Typecasting a [[Nil|nil pointer]] will not raise an exception. | |
| 16 | + | ||
| 17 | + | However, trying to de-reference `nil` by attempting to access an attribute or method *will* cause a [[runtime error|RTE]]. | |
| 18 | + | ||
| 19 | + | ## application | |
| 20 | + | ||
| 21 | + | `as` ensures a typecast is legit. | |
| 22 | + | ||
| 23 | + | `as` is one of the operators that can not be [[Operator overloading|overloaded]]. | |
| 24 | + | ||
| 25 | + | ## see also | |
| 26 | + | ||
| 27 | + | - [[Is|is]] | |
| 28 | + | ||
| 29 | + | --- | |
| 30 | + | *From the Free Pascal wiki, page "As", as it stood on 2022-08-06 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
