Hacker News

Dada 如何启用内部参考

了解 Dada 如何解决困扰 Rust 和系统编程的自引用数据结构,重新思考安全内部引用的所有权和权限。

2 最小阅读量

Mewayz Team

Editorial Team

Hacker News

数十年来困扰系统编程的自指问题

如果您曾经尝试使用具有严格所有权规则的语言构建图、双向链表或观察者模式,您就会知道其中的痛苦。自引用数据结构(结构的一个部分保存指向同一结构另一部分的指针)是出了名的难以安全表达。 Rust 开发人员多年来一直在努力解决这个问题,他们使用 Pin、不安全块或竞技场分配器来建模在垃圾收集语言中感觉微不足道的模式。 Dada 是由 Niko Matsakis 创建的实验性编程语言,它采用了一种完全不同的方法。通过从头开始重新思考所有权和权限,达达在不牺牲内存安全的情况下实现了内部引用——其影响远远超出了学术好奇心。

什么是内部参考以及它们为何重要?

当数据结构内的字段指向同一结构内的另一个字段时,就会发生内部引用。考虑一个解析器,它保存源字符串和该字符串的切片,或者一个 UI 组件,它存储子窗口小部件列表以及指向当前焦点子窗口小部件的指针。这些模式不断出现在现实世界的软件中:事件系统、文档模型、配置树和工作流引擎都依赖于某种形式的自引用。

在 Python 或 JavaScript 等语言中,垃圾收集以不可见的方式处理簿记。您创建引用,并且运行时确保只要有东西指向它,内存就保持活动状态。但在优先考虑零成本抽象和确定性资源管理的系统语言中,编译器需要证明引用不会比它指向的数据更长寿。这就是事情变得复杂的地方——大多数基于所有权的语言迫使开发人员陷入尴尬的变通办法,这些变通办法掩盖了意图并引入了微妙的错误。

挑战不仅仅是理论上的。构建模块化平台的团队(例如 Mewayz 背后的 207 个模块架构)始终依赖于内部参考。引用同一数据上下文中的记录的 CRM 模块、将行项目链接回其父文档的发票引擎,或者指向共享状态对象中的实时数据流的分析仪表板:所有这些都是大规模运行的内部引用模式的真实实例。

传统所有权模式的不足之处

Rust 的借用检查器是现代语言设计中最著名的创新之一,它在编译时消除了所有类别的内存错误。然而,其严格的单一所有者、借用或移动语义使得内部引用确实令人痛苦。当结构体在内存中移动时,任何内部指针都会变得无效。 Rust 的答案——1.33 版本中引入的 Pin API——提供了一种保证值不会移动的机制,但它将复杂性分层到了本应是简单的建模任务上。

💡 您知道吗?

Mewayz在一个平台内替代8+种商业工具

CRM·发票·人力资源·项目·预订·电子商务·销售点·分析。永久免费套餐可用。

免费开始 →

开发人员经常报告说,他们花费 30-40% 的时间与借用检查器对抗涉及自引用的模式。竞技场分配库,如类型化竞技场和基于索引的方法(将索引存储到 Vec 而不是实际引用中)是实用但不完美的解决方案。它们用直接引用的表达能力来换取编译器可以验证的间接引用,但它们也用清晰度来换取样板文件。

“最好的语言功能是使正确的模式成为最容易编写的模式。当开发人员诉诸变通办法时,这意味着语言的模型和他们的思维模型已经出现了分歧。” — Niko Matsakis,谈达达主义背后的设计哲学

达达基于许可的所有权方法

达达主义将所有权重新想象为一系列权限,而不是二元的拥有或借用决策。 Dada 不转移所有权或创建临时借用,而是允许值携带权限注释,描述您可以使用该值做什么

Frequently Asked Questions

What exactly is the "self-referential problem"?

The self-referential problem occurs when a data structure contains a reference to itself, like a node in a graph pointing to another node within the same structure. In languages with strict ownership rules like Rust, this creates a conflict: the language's safety guarantees can't easily determine if the reference will outlive the data it points to. This makes seemingly simple patterns, common in Mewayz's 207+ modules, surprisingly difficult and unsafe to implement.

How does Dada solve this problem differently from Rust?

While Rust often requires complex workarounds like Pin or unsafe code to handle self-references, Dada bakes a solution directly into its ownership model. Dada introduces the concept of "leases," which are temporary, permission-based references. This allows the compiler to statically guarantee the safety of internal pointers without needing special types or breaking memory safety, making it far more ergonomic for these common patterns.

Can I use Dada for my projects today?

Dada is currently an experimental language and not yet ready for production use. It is a research project exploring new ideas in ownership. For robust, production-ready systems programming, Rust remains the leading choice. For higher-level application needs, a service like Mewayz ($19/mo) provides a vast library of pre-built modules to accelerate development without grappling with low-level memory concerns.

Does Dada's approach have any limitations?

Dada's lease system is designed for a specific class of problems involving internal references within a single ownership tree. While it elegantly solves the paradigmatic graph and observer pattern issues, it may not be a silver bullet for all complex pointer scenarios. The model is still under development, and its full capabilities and constraints will become clearer as the language evolves.

Streamline Your Business with Mewayz

Mewayz brings 207 business modules into one platform — CRM, invoicing, project management, and more. Join 138,000+ users who simplified their workflow.

Start Free Today →

免费试用 Mewayz

集 CRM、发票、项目、人力资源等功能于一体的平台。无需信用卡。

立即开始更智能地管理您的业务

加入 30,000+ 家企业使用 Mewayz 专业开具发票、更快收款并减少追款时间。无需信用卡。

觉得这有用吗?分享一下。

准备好付诸实践了吗?

加入30,000+家使用Mewayz的企业。永久免费计划——无需信用卡。

开始免费试用 →

准备好采取行动了吗?

立即开始您的免费Mewayz试用

一体化商业平台。无需信用卡。

免费开始 →

14 天免费试用 · 无需信用卡 · 随时取消