Hacker News | 📄 原文链接 | 2026-10-19 收录

Java 密封类与穷尽模式匹配的代价 — Hacker News 中文摘要

来源:neilmadden.blog — 31 分

📋 概述

Java 17 的密封类允许显式列出子类型,于是 switch 可以省掉 default 并获得编译期穷尽性检查。Neil Madden 提醒这只是硬币的一面:给密封类型加一个新子类型,对使用者就是破坏性变更,按 SemVer 得升大版本;更麻烦的是如果使用者没有跟着重新编译,运行到新分支时会直接抛 MatchException。

🔑 核心要点

💡 金句

sealed types are probably most useful within the implementation of a component, and are less useful when exposed in the public API that a component offers to other components
← 返回 Hacker News 首页