Section 9/91 menit
9. Ringkasan
9. Ringkasan
Decision Guide
swift
Type-mu punya resource yang harus di-release tepat sekali?
├── YA → Pertimbangkan ~Copyable + deinit untuk cleanup
│ Tanyakan: "Apa yang terjadi jika type ini di-copy?"
│ ├── "Double free/close/release" → HARUS ~Copyable
│ └── "Tidak masalah" → Copyable biasa
└── TIDAK → Gunakan Copyable biasa (default)
Type-mu merepresentasikan "kepemilikan eksklusif" atas sesuatu?
├── YA → ~Copyable adalah pilihan natural
└── TIDAK → Copyable biasa
Type-mu perlu conform ke Hashable/Equatable/Codable?
└── YA → Tidak bisa ~Copyable (protokol-protokol ini butuh Copyable)
Perbandingan dengan Alternatif
| Pendekatan | Pro | Cons |
|---|---|---|
~Copyable |
Compile-time safety, zero overhead | Learning curve, limitasi protocol conformance |
class + deinit |
Familiar, flexible | Reference semantics, bisa di-copy pointer |
actor |
Thread-safe, familiar | Async overhead, bukan untuk resource ownership |
Manual (defer { resource.close() }) |
Simple | Error-prone, mudah lupa |
withResource { } pattern |
Explicit scope | Butuh closure, nesting bisa dalam |
Dibuat: 2026-05-08 | Swift 6.0