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



目录
相关文章
|
4月前
|
中间件
在 Egg.js 应用中,你可以通过 `this` 关键字来获取上下文
在 Egg.js 应用中,你可以通过 `this` 关键字来获取上下文
64 1
|
2月前
|
JSON Dart 安全
Flutter Dart Macro 宏简化 JSON 序列化
今天我们将会体验 dart 语言新特性 macro 宏,来实现对 json 的序列化,用到的包是官方实验室写的 json 包。 本文将会一步步的带你实现这个功能,那我们开始吧。
Flutter Dart Macro 宏简化 JSON 序列化
|
3月前
|
Rust 编译器
Rust代码组织:Package、Crate、Module
Rust代码组织:Package、Crate、Module
|
4月前
|
开发者 容器
Spartacus 里动态创建 Injector 的例子
Spartacus 里动态创建 Injector 的例子
|
4月前
|
JavaScript 前端开发 开发者
js代码中“use strict” 是什么意思? 使用它的区别是什么?
js代码中“use strict” 是什么意思? 使用它的区别是什么?
56 1
|
4月前
|
JavaScript 前端开发 开发者
js代码中“use strict” 是什么意思? 使用它的区别是什么
js代码中“use strict” 是什么意思? 使用它的区别是什么
48 1
|
4月前
|
Rust 编译器
【Rust】——package、crate、定义Module
【Rust】——package、crate、定义Module
|
存储 JavaScript 编译器
TypeScript(一) —— 进阶(TypeScript 中的类型、编译选项及使用 webpack 打包 ts 代码)
TypeScript(一) —— 进阶(TypeScript 中的类型、编译选项及使用 webpack 打包 ts 代码)
250 0
|
JavaScript
|
JavaScript 前端开发
浅谈JS中call()和apply()的区别和用途?
前言: 在JavaScript中,this指向问题一直是一个老生常谈的问题。很多小伙伴应该都知道在js中,this指向哪里通常是在函数调用的时候才确定的,简单来说就是谁调用了函数则this指向谁,当然,这只是狭义的,更加详细的介绍大家可以去看《你不知道的Javascript》这本书。 call()和apply()这两个方法的作用可以简单归纳为改变this指向,从而让我们的this指向不在是谁调用了函数就指向谁。
151 1
浅谈JS中call()和apply()的区别和用途?