Skip to content

言語: with / defer によるリソース管理 #102

Description

@ryuichi1208

目的

ファイル・ソケットなど Drop が必要なリソースに対し、明示的に「スコープ終了時の後処理」を書ける構文を導入する。

背景

設計選択肢

方式 特徴
Python with with open("a") as f: コンテキストマネージャ プロトコル
Go defer defer close(f) 関数末尾で逆順実行、明示的
Rust Drop let f = ...; // 自動 暗黙、所有権が前提
Zig defer defer f.close() Go 風だがスコープ末で実行

提案 (drafting)

第一歩としては defer 文(Go/Zig 風) が rw の現状(所有権なし・GC 議論未決)に最も馴染む。

def read_file(path: str) -> Bytes:
    f: File = file_open(path)
    defer file_close(f)
    return file_read_all(f)

将来 #15 / #67 (メモリ管理 RFC) で所有権が入ったら Drop 自動化に移行可能。

受け入れ条件

関連

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:lang言語仕様・構文・型システムenhancementNew feature or requestpriority:high実用化に必須

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions