Section 11/111 menit

11. Ringkasan

11. Ringkasan

Decision Guide

swift
Fungsi ini perlu COPY input?
├── TIDAK perlu (hanya baca) → borrowing
│   Contoh: serialize, analyze, validate, render
│
└── YA perlu modifikasi/ownership
    │
    Caller butuh nilai setelah call?
    ├── TIDAK → consuming (move semantics)
    │   Contoh: submit, publish, build(), export()
    │
    └── YA → Default copy (tidak ada annotation)
        atau inout jika butuh modifikasi in-place

Performance Impact

Scenario Tanpa Annotation Dengan borrowing Saving
Serialize 10MB struct Copy 10MB 0 copy 100%
Render frame 60fps Copy per frame 0 copy 100%
ML inference 100MB model Copy 100MB 0 copy 100%
Small Int/Bool Copy 8 bytes Copy 8 bytes ~0%

Aturan Praktis

swift
// Tipe kecil (primitif, small struct) → tidak perlu annotation
func isValid(_ count: Int) -> Bool { count > 0 }

// Tipe besar yang hanya dibaca → borrowing
func analyze(_ data: borrowing LargeMatrix) -> Analysis { }

// Builder/pipeline → consuming
consuming func filtered(_ predicate: Predicate) -> QueryResult { }

// Noncopyable → harus borrowing atau consuming
func use(_ handle: borrowing FileHandle) { }
func close(_ handle: consuming FileHandle) { }

Dibuat: 2026-05-08 | Swift 6.0