Clojure命名空间中use与require的区别
来源:互联网 发布:ecshop商城源码 编辑:程序博客网 时间:2024/06/09 01:04
这个问题的答案来自于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
- Clojure命名空间中use与require的区别
- Node.js 全局变量命名空间与命名空间中require模块的冲突
- perl的Use与require区别
- perl中use 和 require的区别
- 关于clojure的ns中的require,use,import等
- require和use 的区别
- Clojure命名空间
- 在Perl中Require 和 use 的区别
- PHP中include()与require()的区别
- php中include()与require()的区别
- php中require与include的区别
- PHP中require与include的区别
- PHP中include()与require()的区别
- php中require()与include()的区别
- PHP中include()与require()的区别
- PHP中include与require的区别
- php中 inlcude 与 require 的区别
- php中include与require的区别
- 一个Mina实例
- httpmime-4.1.3 简单使用
- 监听外拨电话
- JSON与XML
- test
- Clojure命名空间中use与require的区别
- ActionBar - Action View
- Zookeeper全解析——Paxos作为灵魂
- 关于网站建设营销内容及时更新的几个知道
- 哪个啥
- Quartz 各种绘制图形用法
- Apache简介
- 【eclipse】如何设置显示代码行数
- 普元EOS简单运算逻辑的编写