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

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

问题描述

我无法理解 clojure 的意图。 spec

它能解决什么样的问题?



为什么要使用它?

解决方案

创建数据和功能的规范。规范是他们的核心谓词(基于现有的Clojure谓词)和结构化的,而不是像静态类型语言中那样基于类型。通过基于谓词的规范,您可以编写比大多数类型系统更具表现力的规范,并使用与您的代码相同的语言。



在函数上定义的规格指定规范的参数,返回值和args 函数的返回值。最后一个允许检查比大多数类型或合约系统可以检查的范围更广的范围(容易)。



定义规格后,您可以使用他们到:


  • 根据规范检查值是否有效

  • Conforma值给你一个解析和解构的值版本
  • 详细解释为什么一个值不符合规范(作为字符串,标准输出或数据)
  • >
  • 使用描述性规范增强功能文档
  • 从规范生成示例数据
  • 声明开发中的一致性,但关闭它们生产

  • 检测开发过程中的无效调用或测试已检测函数

  • 为指定函数生成并运行基于属性的测试
  • >
  • 开发将测试和测试生成与存根和模拟设施相结合的测试



您可以使用spec来改进哟你的测试(通过澄清和记录你的意图,捕获无效的调用,以及声明数据有效性),你的测试(捕获无效调用,声明有效性,生成示例数据,并为你指定的函数生成自动测试)通过使用一致性来解构)。另外,Clojure核心使用规范将导致更好的错误消息,并且扩展了核心库使用的开发时间检查以更早地发现错误。

I could not understand the intent of clojure.spec

What kind of problems does it solve?

Why should we use it?

解决方案

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:

  • Check whether a value is valid according to a spec
  • "Conform" a value which gives you a parsed and destructured version of the value
  • Explain in detail why a value does not conform to a spec (as a string, to stdout, or as data)
  • Enhance function docs with descriptive specs
  • Generate example data from a spec
  • Assert conformance in development but turn them off in production
  • Detect invalid calls in development or test for instrumented functions
  • Generate and run property-based tests for a spec'ed function
  • Develop tests that combine instrumentation and test generation with stubbing and mocking facilities

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).

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天全站免登陆