As: what changed
Revision 1 to 2. 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 | ``` | |
| 7 | + | super(child) | |
| 8 | + | if not assigned(child) and_then not child is super then | |
| 9 | + | begin | |
| 10 | + | raise exception.create(sErrInvalidTypecast); | |
| 11 | + | end; | |
| 12 | + | ``` | |
| 14 | 13 | ||
| 15 | - | ||
| 14 | + | [](</File:Warning-icon.png>) | |
| 16 | 15 | ||
| 17 | - | ||
| 16 | + | **Warning:** Typecasting a [[Nil|`nil` pointer]] will not raise an exception. | |
| 18 | 17 | ||
| 18 | + | However, trying to de-reference `nil` by attempting to access an attribute or method _will_ cause a [[runtime error|RTE]]. | |
| 19 | + | ||
| 19 | 20 | ## application | |
| 20 | 21 | ||
| 21 | - | ||
| 22 | + | `as` ensures a typecast is legit. | |
| 22 | 23 | ||
| 23 | - | ||
| 24 | + | `as` is one of the operators that can not be [[Operator overloading|overloaded]]. | |
| 24 | 25 | ||
| 25 | 26 | ## see also | |
| 26 | 27 | ||
| 27 | - | ||
| 28 | + | * [[Is|`is`]] | |
| 28 | 29 | ||
| 29 | 30 | --- | |
| 30 | - | ||
| 31 | + | *From the Free Pascal wiki, page "As", as the Internet Archive kept it on 2024-03-04. 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).* |
