Repository navigation
TryCast
Home › Interfaces › TryCast
Converts to an interface or a typed pointer if the object really is one, and answers null if it is not.
result = trycast( interface, expression )
result = trycast( typename ptr, expression )-
interface: the interface wanted. -
typename ptr: a pointer to the type wanted. -
expression: an interface value, or a pointer or reference to an object whose type extendsobject.
With an interface: a value of that interface referring to the same object, or a null interface value (equal to nullptr) when the object does not implement it.
With a pointer type: a pointer to the object, or nullptr when the object is not of that type or one derived from it.
trycast asks the object at run time. It is the checked counterpart of Cast: cast( Note ptr, v ) takes the programmer's word for it, and a wrong one is undefined behaviour; trycast( Note ptr, v ) looks, and answers nullptr when the object is something else.
From an interface value it does two things no other operator does:
-
A cross cast:
trycast( ISize, v )asks whether the object behind anIShowalso implementsISize, and if so gives anISizefor the same object. -
A downcast to the concrete type:
trycast( Note ptr, v ).
interface IShow
function Show( ) as ustring
end interface
interface ISize
function Size( ) as int32
end interface
class Note
implements IShow, ISize
declare function Show( ) as ustring implements IShow.Show
declare function Size( ) as int32 implements ISize.Size
as ustring text = "remember the milk"
end class
function Note.Show( ) as ustring
return text
end function
function Note.Size( ) as int32
return len( text )
end function
class Label
implements IShow
declare function Show( ) as ustring implements IShow.Show
end class
function Label.Show( ) as ustring
return "label"
end function
sub Describe( byval v as IShow )
print v.Show( );
dim as ISize sz = trycast( ISize, v ) '' does this object ALSO implement ISize?
if sz <> nullptr then print ","; sz.Size( ); " characters";
dim as Note ptr np = trycast( Note ptr, v ) '' is it a Note?
if np <> nullptr then print " (a Note)";
print
end sub
dim as Note n
dim as Label l
Describe( n )
Describe( l )remember the milk, 17 characters (a Note)
label
Converting up never needs trycast: a Note converts to IShow implicitly, and an interface to any interface it inherits. Converting out of an interface implicitly is an error that suggests trycast (TKO1009).
- The target must be an interface or a pointer to a type (TKO1021).
-
trycastis a run-time search through a small table, so test once and keep the result rather than casting inside a hot loop. -
expression is typenameis the yes/no form of the same question; seeOperator Is. When the answer is known at compile time the compiler says so (TKO1016).
Category: Interfaces Kind: operator
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Home
- What Is tiko-lang
- Quick Start
- Installing on macOS
- Debugging
- Feature Index
- Compatibility
- FAQ
- AI Cheatsheet
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Lambdas
- Block Strings
- Partial Types
- String Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Time Zones
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- PsControls
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing