Repository navigation
Type Temporary
Home › Type (Temporary)
Creates a temporary copy of a user defined type
result = Type( initializers, ... )
result = Type<typename>( initializers, ... )initializerstypename
Used to create a temporary type. If typename is not explicitly given, it will be inferred from its usage if possible. Usage of the temporary copy may include assigning it to a variable, passing it as a parameter to a procedure, or returning it as a value from a procedure.
For a type without own or inherited constructor (excluding also any type that is directly or indirectly derived from Object), the temporary type syntax is allowed if all type data-fields (including those inherited) are numeric primitives only and without any default initializers.
If at same time the type is without destructor, the compiler does a direct assignment instead of using a temporary copy.
The Constructor for the type, if there is one with parameters matching with the initializers provided, will be called when the temporary copy is created, and the Destructor for the type, if there is one, will be called immediately after its use. But when there is a matching constructor, the temporary type expression may be simply replaced by typename( initializers, ... ).
If there is a constructor at least but none which matches with the initializers, the temporary type syntax is obviously disallowed.
It can create not only a temporary copy of an user defined type, but also a temporary copy of predefined data-type as a variable-length string or any numeric data-type (all standard data-types excluding fixed-length strings).
It can also be used as an even shorter shortcut than With (see below) if you are changing all the data-fields (or the n firsts only).
A temporary object is destroyed at the end of execution of the statement (where it's defined), but its corresponding allocated memory is not released and remains available (unused) until going out the scope where statement is.
In the particular case where a temporary type is used in a With expression, the destruction of the temporary type is deferred to the end of the With scope.
Note: Static qualifier used at procedure definition level does not apply to temporary types.
A temporary copy of the type.
- New to FreeBASIC
Type Example
As Integer field1
As Integer field2
End Type
Dim myexample As Example
'' Filling the type by setting each field
myexample.field1 = 1
myexample.field2 = 2
'' Filling the type by setting each field using WITH
With myexample
.field1 = 1
.field2 = 2
End With
'' Fill the variable's fields with a temporary type
myexample = Type( 1, 2 )'' Passing a user-defined types to a procedure using a temporary type
'' where the type can be inferred.
Type S
As Single x, y
End Type
Sub test ( v As S )
Print "S", v.x, v.y
End Sub
test( Type( 1, 2 ) )'' Passing a user-defined type to a procedure using temporary types
'' where the type is ambiguous and the name of the type must be specified.
Type S
As Single x, y
End Type
Type T
As Integer x, y
End Type
Union U
As Integer x, y
End Union
'' Overloaded procedure test()
Sub test Overload ( v As S )
Print "S", v.x, v.y
End Sub
Sub test ( v As T )
Print "T", v.x, v.y
End Sub
Sub test ( v As U )
Print "U", v.x, v.y
End Sub
'' Won't work: ambiguous
'' test( type( 1, 2 ) )
'' Specify name of type instead
test( Type<S>( 1, 2 ) )
test( Type<T>( 1, 2 ) )
test( Type<U>( 1 ) )Kind: keyword
Reproduced from the FreeBASIC Wiki Manual under the GNU Free Documentation License 1.3 — with no Invariant Sections, no Front-Cover Texts and no Back-Cover Texts. Copyright © the FreeBASIC project and wiki contributors. See License.
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- 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)
- 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