Repository navigation
Category Move Semantics
Home › All Categories › Move Semantics
#import "std/containers"
using containers
function LoadNames( byref path as string ) as Array( of string )
dim as Array( of string ) names
' ... fill it ...
return names ' MOVED: the caller gets these buffers
end function
dim as Array( of string ) a = LoadNames( "names.txt" ) ' no copy
dim as Array( of string ) b = move( a ) ' no copy; a is now empty
b.Push( "one more" )A move hands an object's contents to another object without copying them. The heap
data a string or an array owns stays where it is; only the object's own header (a
string's 24 bytes, an Array's 80) goes to its new place, and the source is left empty.
tiko moves automatically wherever the source cannot be used again, and move( x ) asks
for it explicitly.
| Code | What happens |
|---|---|
return r where r is a local variable |
r is moved into the caller's result; its destructor does not run |
return f( ), return a + b
|
the temporary result is moved into the caller's result |
Take( f( ) ) with a byval parameter |
the temporary is the argument: no copy |
b = f( ) |
b's old value is destroyed and the temporary moved in, when the type has no operator let of its own anywhere in it |
Everything else copies, as it always did: returning a shared variable, a byref
parameter, a field or an array element, and assigning from a variable. An assignment
over a type with a hand-written operator let still runs that let.
move( x ) moves out of a variable, a field, an array element or *p that you are done
with. x is reset to its default state -- for a string or an array, empty; for a type
with a constructor, freshly default-constructed -- and is fully usable again:
dim as Array( of long ) work = Build( )
results.Push( move( work ) ) ' no copy
work.Push( 1 ) ' fine: work is an empty array againAn explicit move skips the copy constructor and operator let: you asked for it.
move( x ) is not a reserved word. A procedure, method or field of your own called move
keeps its meaning, and then move( x ) is your procedure.
| Diagnostic | When |
|---|---|
| TKO1274 |
x is not an object of a type: a long, a string
|
| TKO1275 |
x is const -- a move writes to its source |
| TKO1276 (warning) |
x is a temporary, which is moved already |
| TKO1277 |
x's type is nomove
|
A type whose copy constructor and operator let are private cannot be copied -- and
can be moved, because a move calls neither. So it can be returned, kept in a container
and handed on:
#import "std/memory"
function OpenLog( ) as memory.UniquePtr( of LogFile )
dim as memory.UniquePtr( of LogFile ) f = memory.MakeUnique( of LogFile )( )
return f ' moved
end function
dim as memory.UniquePtr( of LogFile ) lg = OpenLog( )
dim as memory.UniquePtr( of LogFile ) other = lg ' error: a copy
dim as memory.UniquePtr( of LogFile ) mine = move( lg ) ' fine: lg is null nowSee Smart Pointers for UniquePtr.
Some objects must stay where they are, because something else holds their address: a
WaitGroup that workers were given @wg of, a node that registered @this in a list.
Mark such a type nomove:
type Pinned nomove
as int32 v
end typeThen move( x ) (TKO1277), swap (TKO1278) and redim preserve (TKO1279) are refused,
and the automatic moves above copy instead. A type with a nomove field is nomove too,
and a container that grows by reallocating -- containers.Array -- cannot hold one.
tasks.WaitGroup is nomove.
Array.Push / Insert, Deque.PushBack / PushFront, LinkedList.PushBack /
PushFront, Map.Put / Add and Channel.Send take their value by value and move it
into place. Passing a temporary or move( x ) copies nothing; passing a variable copies it
exactly once, as before. Channel.Receive moves the value out of the buffer.
FreeBASIC has no moves: a return r copies r, and an assignment from a function result
runs operator let. tiko's moves change how many times a copy constructor, let and
destructor run -- which is the point -- so a program that counts those calls sees fewer
of them than under fbc. Everything else a program can observe is unchanged.
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
- Android (experimental)
- Debugging
- Feature Index
- Compatibility
- FAQ
- AI Cheatsheet
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Move Semantics
- Match
- Lambdas
- Block Strings
- String Interpolation
- Partial Types
- String Slices
- Array 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
- Hardware
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- tiko test
- tiko doc
- 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