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

查看:19
本文介绍了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天全站免登陆