什么是 Clojure 规范? [英] What is Clojure spec?

查看:28
本文介绍了什么是 Clojure 规范?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解clojure的意图.规范

它解决了什么样的问题?

What kind of problems does it solve?

我们为什么要使用它?

推荐答案

spec 允许您为数据和函数创建规范.规范的核心是谓词(基于现有的 Clojure 谓词)和结构,而不是您在静态类型语言中可能看到的基于类型.通过基于谓词的规范,您可以编写比大多数类型系统更具表现力的规范,并且使用与您的代码相同的语言.

spec allows you to create specifications for data and functions. Specifications are at their core predicative (based on existing Clojure predicates) and structural, rather than type-based as you might see in a statically typed language. By basing spec on predicates, you can write specifications that are far more expressive than most type systems and using the same language as your code.

在函数上定义的规格指定参数的规格、返回值以及参数的函数返回值.与大多数类型或合同系统相比,最后一个允许检查范围更广的事物(轻松).

Specs defined on a function specify the specs for the args, the return value, and a function of the args and the return. The last one allows for checking a far greater range of things (easily) than can be checked in most type or contract systems.

定义规范后,您可以使用它们:

Once you have defined specs, you can use them to:

  • 根据规范检查值是否有效
  • 符合"一个值,它为您提供该值的解析和解构版本
  • 详细解释为什么值不符合规范(作为字符串、标准输出或数据)
  • 使用描述性规范增强功能文档
  • 从规范生成示例数据
  • 在开发中声明一致性,但在生产中关闭它们
  • 检测开发或测试中的无效调用以检测检测功能
  • 为指定的函数生成并运行基于属性的测试
  • 开发将检测和测试生成与存根和模拟设施相结合的测试

您可以使用规范来改进您的开发(通过阐明和记录您的意图、捕获无效调用和断言数据有效性)、您的测试(捕获无效调用、断言有效性、生成示例数据并为您的规范生成自动测试'ed 函数),以及您的产品(通过使用一致性进行解构).

You can use spec to improve your development (by clarifying and documenting your intent, catching invalid calls, and asserting data validity), your testing (catch invalid calls, assert validity, generate example data, and generate automatic tests for your spec'ed functions), and your production (by using conformance for destructuring).

此外,Clojure 核心对规范的使用将导致更好的错误消息和扩展核心库使用的开发时检查以及早发现错误.

Additionally, Clojure core's use of specs will lead to better error messages and expanded development-time checking of core library use to find errors earlier.

这篇关于什么是 Clojure 规范?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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