Files
coollang/samples/01_capability_signature.cool
T
coolguyandClaude Opus 5 e3b19b1300 thesis+samples: 표기 결정 6건 반영과 문법 탐침 추가
샘플을 먼저 써서 EBNF 이전에 체감을 확인했고, 거기서 강제된 결정을 문서에
되먹인다.

- callable 파라미터의 기본을 use(비탈출)로 뒤집고 저장하는 쪽을 own fn으로
  유표기. 반대로 두면 클로저를 저장하지 않는 고차 함수 — 표준 라이브러리의
  거의 전부 — 가 use로 도배된다. 무표기-흔함/유표기-위험은 effects 표기에
  이미 있는 원칙이라 새 조항이 아니다.
- 재수출 키워드를 reexport로 확정. export는 "처음 내보내기"와 흐려지는데
  둘은 hash 규칙에서 의미가 다르고, use는 capability 전달 전용으로 남긴다.
- effects 절을 파라미터 목록 뒤·화살표 앞으로 이동. 중첩 함수 타입의 구문
  모호성이 어순으로 사라지고 읽기 순서가 "입력과 권한 → 출력"이 된다.
  선언과 타입 표기를 같은 어순으로 통일.
- effect 위치 제한을 검사기에서 구문 수준으로 격상. 파라미터 위치는 변수
  단독 또는 리터럴 집합만, 합집합은 결과 위치 전용 프로덕션.
- scope는 이름을 갖는 형태로 확정. 중첩에서 바깥 스코프에 붙이는 정당한
  패턴이 암묵 바인딩으로는 표현되지 않는다.
- capability struct를 표준 관용구로 문서화. named effect set과 scope
  축약형은 v1 예약.

samples/는 명세가 아니라 탐침이다. 05는 통과하면 안 되는 파일이며 체커가
생기면 그대로 테스트 케이스가 된다.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019ZVDeU6KLuUVL3gs18Hm3E
2026-08-30 01:35:30 +09:00

54 lines
1.5 KiB
Plaintext

// 01. use 파라미터 + effects 절이 한 시그니처에 동시에 붙는 모양
//
// 어순: fn 이름(파라미터) effects {...} -> 반환타입
// 검증 대상: capability를 받는 평범한 함수가 사람이 읽을 만한가.
import "cool.dev/std/list" as List
pub fn refund_order(
use pay: PaymentGateway,
id: OrderId,
) effects {PaymentGateway.refund} -> Result<Receipt, PayError> {
pay.refund(id)
}
// capability 둘, effect 둘.
pub fn refund_and_log(
use pay: PaymentGateway,
use log: Logger,
id: OrderId,
) effects {PaymentGateway.refund, Logger.write} -> Result<Receipt, PayError> {
let receipt = pay.refund(id)?
log.write("refunded: ", id)
Ok(receipt)
}
// capability 셋 이상은 struct로 접는다 (표준 관용구).
// Deps는 affine 필드를 가지므로 전이적으로 affine이고, use로 빌려 쓴다.
pub struct Deps {
pay: PaymentGateway,
log: Logger,
db: Database,
}
pub fn checkout(
use deps: Deps,
order: Order,
) effects {PaymentGateway.charge, Database.write, Logger.write} -> Result<Receipt, Error> {
let receipt = deps.pay.charge(order.id, order.amount)?
deps.db.write(receipt)?
deps.log.write("checkout: ", order.id)
Ok(receipt)
}
// use 값을 다른 use 파라미터로 넘기는 것은 허용된다 (복제가 아니다).
pub fn refund_all(
use pay: PaymentGateway,
ids: List<OrderId>,
) effects {PaymentGateway.refund} -> Result<Unit, PayError> {
List.each(ids, fn(id) {
refund_order(pay, id)?
Ok(unit)
})
}