如何避免在Scala中编写令人困惑的DSL [英] How to avoid writing confusing DSLs in Scala

查看:91
本文介绍了如何避免在Scala中编写令人困惑的DSL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了评论,指出Scala的灵活性使开发人员可以轻松编写难以理解和推理的DSL。

I've read comments stating that Scala's flexibility makes it easy for developers to write DSLs that are difficult to understand and reason about.

DSL之所以可能,是因为

DSLs are possible because


  • 我们可以有时省略和括号(例如 List(1)map println

  • 我们可以< a href = https://stackoverflow.com/q/4386127/5986907>有时交换() {}

  • 我们有隐式值,参数和类(还有转换,它们是现在灰心

  • 有一个数字相对较小语言中保留符号的集合(例如,我可以为我的班级定义 +

  • we can sometimes omit . and parentheses (e.g. List(1) map println)
  • we can sometimes interchange () and {}
  • we have implicit values, parameters, and classes (also conversions, which are now discouraged)
  • there is a relatively small number of reserved symbols in the language (e.g. I can define + for my class)

以及其他语言功能。

如何避免编写令人困惑的DSL ...常见的反模式?哪里不适合使用DSL?

How can I avoid writing confusing DSLs ... what are the common antipatterns? Where is a DSL not appropriate?

推荐答案

无论何时创建自己的DSL,您都将嵌入 new 语言转换为Scala(不是标准),因此不会遵循标准代码指南,约定等。

Whenever you create DSL of your own you're embedding new language into Scala, which is not standard, so it doesn't follow standard code guides, conventions, etc.

我想说,只要添加适当的文档并说明创建目的,添加新的DSL并没有错。并添加用法示例。如果您觉得添加新的DSL可以提高代码的可读性,那么就去做吧,但是请记住,只要有人遇到您的DSL且文档记载不足,他们就会非常困惑。

I would say it's nothing wrong with adding new DSL as long you add proper documentation, explain the purpose of creating it and add examples of usage. If you feel adding new DSL would increase readability of your code, just go for it, but remember that whenever anyone encounters your DSL and it won't be documented enough, they will be very confused.

scalatest匹配器 scala持续时间

这篇关于如何避免在Scala中编写令人困惑的DSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆