about
3/03/2022
Abstract Types
BasicBites - Abstract Types
Copy, Move, and Reference
- Part 1. Defines abstract type behaviors Copy, Move, and Reference.
- Part 2. Shows how C++, Rust, and C# implement the abstract type behaviors in their own type systems. A description for Java would use almost the same text as used for C#, as C# started out with a fork of the Java JVM, the same type structure, with some language syntax changes.
Part 1. - Abstract Type Behaviors
Copy Types:
Copy construction: | T t1 = 2; | T t2 = t1; | t1 and t2 occupy disjoint blocks of memory with the same value |
Copy assignment: | T t3 = 0; | t2 = t3; | t2 and t3 occupy disjoint blocks of memory with the same value |
Pass by value: | fn f(t:T) { ... } | f(t2) | t2 copied onto f's stackframe |
Move Types:
Move construction: | T t1 = new T(1, "xyz") | T t2 = t1; | t1's resources given to t2, t1 now invalid |
Move assignment: | T t3 = new T(2, "abc"); | t2 = t3; | t3's resources given to t2, t3 now invalid |
Pass by value: | fn f(t:T) { ... } | f(t2) | t2 moved into f, t2 now invalid |
Reference Types:
Handle‑body construction: | T h1 = new T(1, "xyz") | T h2 = h1; | h1's resources - the body - are now attached to both h1 and h2 |
Handle‑body assignment: | T h3 = new T(2, "abc"); | h2 = h3; | Both h2 and h3 point to the same body |
Pass by value: | fn f(h:T) { ... } | f(h3) | Handle h3 copied to f's stackframe; copy points to h3's body |
Part 2. - Language Specific Details
- Copy operation happens when we assign primitive instances in C++, Rust, and C#.
- Move operation is an efficient transfer of ownership of instance resources.
- Reference operations copy handles that refer to instances in the heap.
C++ Specific Types
Copy Types
Move Types
Reference Types
raw pointer,
T* aptr = &t; and reference
T& aref = t;
The C++ reference is not a Reference type, as described in Part 1., but rather a type qualifier that
creates a fixed
binding to its target with a pointer, but uses object syntax to manipulate the target.
auto r = std::unique_ptr<T>(new T(args));
It provides a method move that transfers ownership of
the target to another std::unique_ptr.
auto shareT1 = std::shared_ptr<new(t)>;
auto shareT2 = shareT1;
auto shareT2 = shareT1;
User defined reference types
Type Aliases
- using ID = unsigned int;
- typedef unsigned int ID;
Consequences:
Rust Specific Types
Copy Types
Move Types
Reference Types
let t = T:new(); let boxed = Box::new(t);
boxed is a smart pointer to a copy or move of t in the heap.
User defined reference types
Type Aliases
- type ID = u32;
- use u32 as ID;
Consequences:
C# Specific Types:
-
Value Types:
primitive scalar types, struct and enum
All reside in the stack -
Reference Types:
class, interface, array, and delegate
All reside on the managed heap
Copy Types
Move Types
Reference Types
Type Aliases
- using Records = List<Tuple<int, string, int>>
Consequences:
References:
C++ Types | Informal survey of the C++ type system |
Rust Types | Thorough semi-formal description of the Rust type system |
C# Types | Thorough semi-formal description of the C# type system |
Java Types | Informal survey of the Java type system |