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

查看:100
本文介绍了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#的东西,它大致类似于静态类" ...它是一个实体,可以容纳绑定值和函数以及类型(请注意,名称空间不能直接包含值/函数,名称空间只能包含类型,而类型本身可以包含值和函数).可以通过"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天全站免登陆