// 06. callable affinity — fn과 affine fn의 구분 // // affinity는 타입 속성, own/무표기는 파라미터 위치 속성. 둘은 직교한다. // copyable 환경만 capture → fn. 복사해서 여러 번 써도 된다. pub fn make_formatter(prefix: String) -> fn(String) -> String { fn(s) { String.concat(prefix, s) } } // affine 값을 소유해 가므로 own. 결과 클로저는 자동으로 affine fn이 된다. pub fn deferred_close(own f: File) -> affine fn() effects {File.close} { fn() { File.close(f) } } // affine fn을 호출 후 소비하려면 소유해야 한다. pub fn run_once(own action: affine fn() effects {File.close}) effects {File.close} { action() } pub fn example(own f: File) effects {File.close} { let close = deferred_close(f) run_once(close) // run_once(close) // ERROR: close는 affine이고 앞줄에서 move됨 } // 빌리는 쪽은 무표기다 — 흔한 쪽에 표기가 없다. pub fn with_retry[a, e: effects]( times: Int, body: fn() effects e -> Result[a, Error], ) effects e -> Result[a, Error] pub fn refund_with_retry( pay: PaymentGateway, id: OrderId, ) effects {PaymentGateway.refund} -> Result[Receipt, Error] { with_retry(3, fn() { pay.refund(id) }) }