Skip to Content
《Scala 编程》,第 3 版
book

《Scala 编程》,第 3 版

by Dean Wampler
May 2025
Intermediate to advanced
556 pages
7h 34m
Chinese
O'Reilly Media, Inc.
Content preview from 《Scala 编程》,第 3 版

第 21 章 Scala 中的特定领域语言 Scala 中的特定领域语言

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

特定领域语言(DSL)是一种编程语言,它模仿目标领域专家使用的术语、惯用语和表达方式。用 DSL 编写的代码读起来就像该领域的结构化散文。理想情况下,即使没有编程经验的领域专家也能阅读、理解和验证这些代码,甚至还能用 DSL 编写代码。

对于这个庞大的主题以及 Scala 对它的支持,我们只是浅尝辄止。要了解更深入的内容,请参阅参考书目中的 DSL 参考资料。

精心设计的 DSL 有几个好处:

封装

DSL 隐藏实现细节,只公开与领域相关的抽象概念。

生产率

由于封装了实现细节,DSL 可以优化为应用功能编写或修改代码所需的工作量。

交流

DSL 可帮助开发人员了解领域,并帮助领域专家验证实现是否符合要求。

然而,数字用户线路也有几个缺点:

DSL 难以创建

尽管编写 DSL 已经成为一种潮流,但我们不应低估其中的艰辛。实现技术并非易事。要考虑到所有可能的用户错误,并提供适当的错误处理和直观的反馈是很困难的。好的 DSL 比传统的 API 更难设计。后者往往遵循应用程序接口设计的语言习惯,在这种情况下,统一性非常重要,而且易于遵循。相比之下,由于每个 DSL 都是一种独特的语言,因此很难自由创建反映领域的代码习语。

DSL 难以维护

由于使用的实现技术并不复杂,随着领域的变化,DSL 可能需要更多的长期维护。为了获得更好的用户体验,往往要牺牲实现的简单性。

难以掩盖执行情况

DSL 通常是一种漏洞百出的抽象。尤其是当错误发生时,很难向用户隐藏细节。

然而,精心设计的 DSL 可以成为加快用户工作效率的有力工具。

从实现的角度来看,DSL 可分为内部和外部两种。内部(或嵌入式DSL是在 Scala 等通用编程语言中编写代码的惯用方式。不需要特殊用途的解析器。相比之下,外部 DSL是一种自定义语言,有自己的自定义语法和解析器。

内部 DSL 可能更容易创建,因为它们不需要专门的解析器。另一方面,底层语言的约束限制了表达领域概念的选择,也更难隐藏底层实现。外部 DSL 则消除了这种限制。只要能编写出可靠的解析器,您就可以任意设计语言。使用自定义解析器也很有挑战性。向用户返回良好的错误信息一直是解析器编写者面临的挑战。

内部 DSL

Scala 语法的一些特性支持创建内部(嵌入式)DSL:

灵活的名称规则

由于可以在名称中使用几乎任何字符,因此很容易创建符合领域的名称,例如具有相应属性的类型的代数符号。例如,如果有一个Matrix 类型,就可以用* 方法实现矩阵乘法。

英数符号

如果不能使用 infix 符号,定义* 方法就没有多大意义;例如,matrix1 * matrix2.1

使用子句参数、上下文函数和默认参数值

减少模板和隐藏复杂细节的三个特性,如必须传递给 DSL 中每个方法的上下文,可以用 using 子句或上下文函数代替。回想一下,很多Future 方法都隐含了ExecutionContext 。Scala 3 中新增的上下文函数有助于消除模板,并为构建简洁的功能提供灵活的方法。详情请参阅"上下文函数"

类型类和扩展方法

为现有类型添加方法的能力。例如 scala.concurrent.duration包具有数字的隐式转换功能,允许您编写1.25.minutes ,返回一个 FiniteDuration ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Java 技术手册(原书第7版)

Java 技术手册(原书第7版)

Benjamin J. Evans, David Flanagan
Spark GraphX in Action

Spark GraphX in Action

Michael Malak, Robin East
Strata Data Superstream Series: Data Warehouses, Data Lakes, and Data Lakehouses

Strata Data Superstream Series: Data Warehouses, Data Lakes, and Data Lakehouses

Alistair Croll, Chris Messina, Michael Armbrust, Rukmani Gopalan, Joyce Kay Avila, Harshida Patel, Victor Lee, Barr Moses, Ryan Kearns, Paul Lacey, Alicia Moniz

Publisher Resources

ISBN: 9798341659063