F# 中的命名空间和模块有什么区别? [英] What the difference between a namespace and a module in F#?

查看:17
本文介绍了F# 中的命名空间和模块有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习 F#(之前几乎没有 .NET 经验)所以请原谅我可能是一个非常简单的问题:F# 中的命名空间和模块之间有什么区别?

I've just started learning F# (with little prior experience with .NET) so forgive me for what is probably a very simple question: What the difference between a namespace and a module in F#?

谢谢

戴夫

感谢布赖恩的回答.这就是我想知道的.澄清一下:你也可以打开一个命名空间(类似于 C# using 语句)吗?

Thanks for the answer Brian. That's what I wanted to know. Just a clarification: can you also open a namespace as well (similar to C# using statement)?

推荐答案

命名空间是 .Net 的东西,在许多工业级语言中很常见,只是一种组织框架和避免不同库之间命名冲突的方法.你和我都可以定义一个类型Foo"并在一个项目中使用它们,只要它们位于不同的命名空间中(例如 NS1.Foo 和 NS2.Foo)..Net 中的命名空间包含类型.

A namespace is a .Net thing, common in many industrial-strength languages, just a way to organize frameworks and avoid naming conflicts among different libraries. Both you and I can define a type "Foo" and use them both in a project, provided they are in different namespaces (e.g. NS1.Foo and NS2.Foo). Namespaces in .Net contain types.

模块是一个 F# 的东西,它大致类似于静态类"......它是一个可以保存 let-bound 值和函数以及类型的实体(注意命名空间不能直接包含值/函数,命名空间只能包含类型,类型本身可以包含值和函数).模块内的事物可以通过ModuleName.Thing"引用,这与命名空间的语法相同,但 F# 中的模块也可以打开"以允许非限定访问,例如

A module is an F# thing, it is roughly analogous to a "static class"... it is an entity that can hold let-bound values and functions, as well as types (note that namespaces cannot directly contain values/functions, namespaces can only contain types, which themselves can contain values and functions). Things inside a module can be referenced via "ModuleName.Thing", which is the same syntax as for namespaces, but modules in F# can also be 'opened' to allow for unqualified access, e.g.

open ModuleName
...
Thing  // rather than ModuleName.Thing

(命名空间也可以类似地打开,但是模块可以包含值和函数这一事实使得打开模块更加有趣",因为您可以以值和函数结束,例如cos",即名称您可以直接使用,而在其他 .Net 语言中,您通常必须对其进行限定,例如Math.cos").

( Namespaces can also similarly be opened, but the fact that modules can contain values and functions makes opening a module more 'interesting', in that you can wind up with values and functions, e.g. "cos", being names you can use directly, whereas in other .Net languages you'd typically always have to qualify it, e.g. "Math.cos").

如果您在 F# 中的顶层"键入代码,则此代码会隐式进入模块中.

If you type in code at 'the top level' in F#, this code implicitly goes in a module.

希望能有所帮助,这是一个非常开放的问题.:)

Hope that helps somewhat, it's a pretty open-ended question. :)

这篇关于F# 中的命名空间和模块有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆