Clojure命名空间中use与require的区别

简介: <p><span style="font-size:18px">这个问题的答案来自于stackoverflow,老外人家回答问题就是细心、认真,学习一下。<br> A:Can anyone explain the difference between use and require, both when used directly and as:use and :require in t

这个问题的答案来自于stackoverflow,老外人家回答问题就是细心、认真,学习一下。
A:Can anyone explain the difference between use and require, both when used directly and as:use and :require in the ns macro?
B:require loads libs (that aren't already loaded), use does the same plus it refers to their namespaces with clojure.core/refer (so you also get the possibility of using :exclude etc like withclojure.core/refer). Both are recommended for use in ns rather than directly.
A:If I require lib foo, then to use bar in foo, I'd have to write foo/bar every time, right? Why would you want to load a lib in ns but then not refer it into the ns? I guess you might be worried about collisions, and you don't want to bother having to reconcile them, right?
B:not having to reconcile collisions is a good point, and more generally there's a programming style which says "namespaces are a honking great idea, we should have more of them" (from "The Zen of Python") -- so e.g. that style recommends not using "using namespace foo;" in C++, so that readers and maintainers of the code won't have to worry "where does this bar come from" but see a more explicit foo::bar instead. require (vs use) supports this "explicit namespaces" style.


总结一下:

use就是在当前项目中引用别的项目时,通过clojure.core/refer把其他项目的函数变量等引入当前项目的工作空间中来,这样在引用其他项目中的函数时就不用再写项目名了。
require比use功能少些,不把别的项目引入当前工作空间,在引用别的项目的函数时需要写全项目名+函数名。

至于其中的原因,老外说的很清楚了。that style recommends not using "using namespace foo;" in C++, so that readers and maintainers of the code won't have to worry 

扩展阅读:

http://stackoverflow.com/questions/3408076/difference-in-clojure-between-use-and-require

http://stackoverflow.com/questions/871997/difference-between-use-and-require



目录
相关文章
|
缓存 C#
C# 操作路径(Path)类方法的使用与解析运行实例
C# 操作路径(Path)类方法的使用与解析运行实例
|
Java API Maven
【现成工具】java获取国家法定节假日包含指定月份节假日和周末
【现成工具】java获取国家法定节假日包含指定月份节假日和周末
3298 0
|
容器
Thymeleaf $/*/#/@语法
Thymeleaf $/*/#/@语法
375 0
|
XML JSON 数据安全/隐私保护
PyMuPDF,Python处理PDF的宝藏库
PyMuPDF,Python处理PDF的宝藏库
455 2
|
Web App开发 人工智能 运维
无缝融入,即刻智能[1]:MaxKB知识库问答系统,零编码嵌入第三方业务系统,定制专属智能方案,用户满意度飙升
【8月更文挑战第1天】无缝融入,即刻智能[1]:MaxKB知识库问答系统,零编码嵌入第三方业务系统,定制专属智能方案,用户满意度飙升
无缝融入,即刻智能[1]:MaxKB知识库问答系统,零编码嵌入第三方业务系统,定制专属智能方案,用户满意度飙升
|
消息中间件 JavaScript 小程序
Controller层代码这么写,简洁又优雅!
说到 Controller,相信大家都不陌生,它可以很方便地对外提供数据接口。它的定位,我认为是「不可或缺的配角」。 说它不可或缺是因为无论是传统的三层架构还是现在的 COLA 架构,Controller 层依旧有一席之地,说明他的必要性。 说它是配角是因为 Controller 层的代码一般是不负责具体的逻辑业务逻辑实现,但是它负责接收和响应请求。
|
JSON 运维 监控
Spring Boot中的健康检查端点配置
Spring Boot中的健康检查端点配置
|
JavaScript 前端开发 开发者
Web Components详解-Shadow DOM基础
Web Components详解-Shadow DOM基础
512 1
|
JavaScript
JS 你可能没用过的【回调函数式替换】replace()
JS 你可能没用过的【回调函数式替换】replace()
177 0

热门文章

最新文章