As: what changed
Revision 2 to 3. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | - | ||
| 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 | 2 | ||
| 3 | 3 | ## operation | |
| 4 | 4 | ||
| 5 | - | ||
| 6 | - | ||
| 7 | - | ||
| 8 | - | ||
| 9 | - | ||
| 10 | - | ||
| 11 | - | ||
| 12 | - | ||
| 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: | |
| 13 | 6 | ||
| 14 | - | ||
| 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 | + | ``` | |
| 15 | 14 | ||
| 16 | - | ||
| 15 | + | > **Warning:** Typecasting a [[Nil|nil pointer]] will not raise an exception. | |
| 17 | 16 | ||
| 18 | - | ||
| 17 | + | However, trying to de-reference `nil` by attempting to access an attribute or method *will* cause a [[runtime error|RTE]]. | |
| 19 | 18 | ||
| 20 | 19 | ## application | |
| 21 | 20 | ||
| 22 | - | ||
| 21 | + | `as` ensures a typecast is legit. | |
| 23 | 22 | ||
| 24 | - | ||
| 23 | + | `as` is one of the operators that can not be [[Operator overloading|overloaded]]. | |
| 25 | 24 | ||
| 26 | 25 | ## see also | |
| 27 | 26 | ||
| 28 | - | ||
| 27 | + | - [[Is|is]] | |
| 29 | 28 | ||
| 30 | 29 | --- | |
| 31 | - | ||
| 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).* |
