Skip to content

Instantly share code, notes, and snippets.

@KyLeggiero
Created October 16, 2023 23:12
Show Gist options
  • Save KyLeggiero/a668a5342d04c8d74a842ba55f8ad561 to your computer and use it in GitHub Desktop.
Save KyLeggiero/a668a5342d04c8d74a842ba55f8ad561 to your computer and use it in GitHub Desktop.
COW Example
//
// main.swift
// COW Example
//
// Created by The Northstar✨ System on 2023-10-16.
//
import Foundation
struct Person {
var name: String
var birthdate: Date
var favoriteNumber: Int
mutating func newFavoriteNumber() {
favoriteNumber = .random(in: 1 ... 1000)
}
}
func main() {
// `bate` allocated & initialized at A1 (memory address A in stack frame 1)
var bate = Person(
name: "Bate",
birthdate: Date(timeIntervalSince1970: 665046000),
favoriteNumber: 42)
// `bate` is read, but not modified
dump(bate, name: "bate")
// `dax` is allocated at A1
var dax = bate
// `dax` is printed by reading the same value in-memory as `bate` at A1
dump(dax, name: "dax")
// B1 is allocated,
// the value of `bate` is copied to it,
// `dax` recontextualized to be at B1,
// field `name` at B1 set to `"Dax"`
dax.name = "Dax"
// field `favoriteNumber` at B1 is set to `7`
dax.favoriteNumber = 7
// field `birthdate` at memory address A1 is set one second later
bate.birthdate += 1
// `bate` at A1 is printed
dump(bate, name: "bate")
// `dax` at B1 is printed
dump(dax, name: "dax")
// Value at A1 passed to the new stack frame
anotherStackFrame(bate)
// `bate` at A1 is printed
dump(bate, name: "bate")
// `dax` at B1 is printed
dump(dax, name: "dax")
}
// `person` is immutable, so its value is that same value which was passed in (for this example, let's say A1)
func anotherStackFrame(_ person: Person) {
// value at A1 is printed
dump(person, name: "person", indent: 1)
// `rando` is allocated at A1
var rando = person
// A2 is allocated,
// the value of the `person` is copied to it,
// `person` recontextualized to be at A2,
// field `name` is set to `"Rando"`
rando.name = "Rando"
// The field `favoriteNumber` at A2 is changed to be a random number
rando.newFavoriteNumber()
// `rando` at A2 is printed
dump(rando, name: "rando")
// value at A1 is printed
dump(person, name: "person")
// Stack frame is popped,
// `rando` is no longer a symbol,
// A2 released for future use
return
}
main()
/*
▿ bate:
- name: "Bate"
▿ birthdate: 1991-01-28 07:00:00 +0000
- favoriteNumber: 42
▿ dax:
- name: "Bate"
▿ birthdate: 1991-01-28 07:00:00 +0000
- favoriteNumber: 42
▿ bate:
- name: "Bate"
▿ birthdate: 1991-01-28 07:00:01 +0000
- favoriteNumber: 42
▿ dax:
- name: "Dax"
▿ birthdate: 1991-01-28 07:00:00 +0000
- favoriteNumber: 7
▿ person:
- name: "Bate"
▿ birthdate: 1991-01-28 07:00:01 +0000
- favoriteNumber: 42
▿ rando:
- name: "Rando"
▿ birthdate: 1991-01-28 07:00:01 +0000
- favoriteNumber: 171
▿ person:
- name: "Bate"
▿ birthdate: 1991-01-28 07:00:01 +0000
- favoriteNumber: 42
▿ bate:
- name: "Bate"
▿ birthdate: 1991-01-28 07:00:01 +0000
- favoriteNumber: 42
▿ dax:
- name: "Dax"
▿ birthdate: 1991-01-28 07:00:00 +0000
- favoriteNumber: 7
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment