Lets say we had something like this:
protocol Delegate {}
struct Value: Delegate {}
class Reference: Delegate {}
struct Test {
let delegate: Delegate
}
How could we know if a delegate is a struct (value type) or a class (reference type)?
First thought that comes to mind is to check memory address equality after making a copy of a delegate:
struct Test {
let delegate: Delegate
var isReferenceType: Bool {
let copy = delegate
let copyAddress = // ... get memory address of a copy
let originalAddress = // ... get memory address of an original
return copyAddress == originalAddress
}
}
- Is it even possible to do this?
- Is there more elegant/correct way of doing this?
- Copying a value type might potentially be an expensive operation?