为什么文件和目录没有单独的命名空间?

发布于 2024-07-22 05:10:38 字数 283 浏览 8 评论 0原文

文件和目录可以有不同的命名空间,但仍然可以用来标识特定的文件,因为同名的文件和目录可以通过不同种类的东西来区分。

原始字段和引用字段也可以有不同的命名空间(在 Java 中),因为如果原始字段和引用字段具有相同的名称,则可以通过不同种类的事物来识别它们。

像这样在其他地方使用单独的名称空间。 例如,在 Java 中,您可以有一个方法 exampleName() 和一个字段 exampleName,虽然它们具有相同的名称,但它们通过不同种类的事物来区分。

Files and directories could have different namespaces, and still be used to identify specific files, because a file and directory with the same name can be distinguished by being different kinds of things.

Primitive field and reference fields could also have different namespaces (in Java), because if a primitive and a reference field had the same name, they could be identified by being different kinds of things.

Separate namespaces are used elsewhere like this. For example, in Java, you can have a method exampleName() and a field exampleName, and though they have the same name, they are distinguished by being different kinds of things.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

剧终人散尽 2024-07-29 05:10:38

首先,这个问题是特定于语言的。 在纯 OOP 语言中,原子元素和复合元素之间没有区别。 一切都是对象。 出于类似的原因,在纯函数式语言中,函数和变量不能具有相同的名称。

其次,如果您有多态操作,则无法知道您引用了哪个变量。 例如,由于多态操作,文件和目录不能有不同的命名空间,例如

cp foo bar

cp 适用于文件和目录,如果您有不同的命名空间,则无法知道您的意思。

First, this question is language specific. In pure OOP languages there's no distinction between atomic and compound elements. Everything is an object. By a similar reason in a pure functional language you can't have function and variable named the same.

Second, if you have polymorphic operations, there's no way to tell which variable did you refer to. For example, you can't have different namespaces for files and directories, because of the polymorphic operations, like

cp foo bar

The cp works on files and dirs, and if you have different namespaces, there's no way to tell what did you mean.

昔梦 2024-07-29 05:10:38

我认为这不是一个好主意。 我想原因涉及文件系统代码的性能和简单性等。 如果目录列表必须沿着 2 或 3 或更多个不同的路径(具体取决于您认为应该拥有多少个不同的命名空间),这可能会使代码变得复杂。

此外,请考虑最终用户可能出现的困惑。 目前,我们通过使用文件扩展名在文件系统中提供了一种命名空间。 您可以将 file.txt、file.dll 和 file.exe 全部存在于同一目录中。 当这些文件同时存在时会发生什么是一个值得关注的问题 - 这是病毒编写者使用某种形式的社会工程来让您单击错误文件的一种方法。 想象一下,如果您也可以将目录与同名文件混淆吗?

I do not believe that this would be a good idea. I imagine that the reasons involve things like performance and simplicity of the filesystem code. If a directory listing had to go down 2 or 3 or more different paths depending on how many different namespaces you think you should have, this would probably complicate the code.

Additionally, consider the end user confusion that might arise. Currently we have a kind of namespacing available in filesystems by using file extensions. You can have file.txt and file.dll and file.exe all existant in the same directory. What happens when these files are present simultaneously is a matter of concern - this has been one method for virus writers to use a form of social engineering to get you to click on the wrong file. Imagine if you could confuse a directory with a file of the same name as well?

旧竹 2024-07-29 05:10:38

目录和文件不一定有很大不同。 它们都是其父目录中的条目,只是带有一个标志来指示条目是否是目录。 您可以打开一个目录并读取它,就像它是一个文件一样,只是可以对其进行某些其他操作 - 符号链接的工作方式相同。 (这个描述偏向于 Unix 文件系统视图,但我认为 DOS/Windows 视图的工作方式大致相同)。 在任何目录中,都有一组成员名称,并且文件系统强制执行唯一性约束,即目录只能有一个具有给定名称的成员。

考虑一下 Java 方法名称与字段名称的比较——回到 C 语言,您不能拥有同名的全局函数和全局变量,因为对象文件中的所有符号都位于单个命名空间中。 但你可以使用 C++,因为函数“void foo()”被映射到损坏的符号名称(“foo__vv”或其他东西)。 因此,与其说它们有一个单独的命名空间,不如说字段“foo”与方法“foo()”的命名空间键是不同的。 鉴于您无法获得关键冲突,它们看起来像是单独的命名空间,但这真的是它的实现方式吗?

Directories and files aren't necessarily so different. They are both entries in their parent directory, just with a flag to indicate if an entry is a directory or not. You can open a directory and read it just as if it was a file, just that certain other operations are possible on it- symbolic links work the same way. (This description is biased towards the Unix filesystem view, but I think the DOS/Windows view works much the same way). Within any directory, there is a set of names of members, and the filesystem enforces the uniqueness constraint that a directory can only have one member with a given name.

Thinking about Java method names compared to field names--- back in C, you couldn't have a global function and global variable with the same name, because all the symbols in the object file are in a single namespace. But you could with C++, because a function "void foo()" was mapped to mangled symbol name ("foo__vv" or something). So it's not so much that they have a separate namespace, as that the key into the namespace is different for a field "foo" vs a method "foo()". Given that you can't get key clashes, they look like separate namespaces, but is that really how it's implemented?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文