F#中的签名文件和访问修饰符 [英] Signature Files and Access Modifers in F#

查看:90
本文介绍了F#中的签名文件和访问修饰符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在尝试学习F#的面向对象方面,并对如何限制对语言中的类型/模块的访问感到好奇。

I've recently been trying to learn the Object-Oriented aspects of F#, and have become curious about how to restrict access to types/modules in the language.

更具体地说,我想知道编写此代码的区别:

More specifically, I want to know the difference between writing this:

Example.fsi

module Stack =
    val foo : string

Example.fs

module Stack =
    let foo = "foo"
    let bar = "bar"

或者另外:

module Stack =
    let foo = "foo"
    let private bar = "bar"

他们最终是否会完成完全相同的事情?来自C#背景,我更倾向于仅在签名(FSI)文件上使用访问修饰符。它们似乎更通用(例如,可以应用于名称空间中的模块/类型),而在任何情况下,签名文件都提供了访问修饰符所不能提供的功能。

Do they not accomplish exactly the same thing in the end? Coming from a C# background, I'm much inclined just to use the access modifiers over signature (FSI) files. They seem to be more versatile (can apply to modules/types in namespaces, for example), whereas I don't any situation in which signature files offer something that access modifiers don't.

推荐答案

他们几乎完成了同一件事。 (请注意,您也可以将.fsi文件用于命名空间中的类型,不确定您对此的评论是什么意思。)

They accomplish almost the same thing. (Note that you can use an .fsi file for types in namespaces too, was unsure what your comment about that meant.)

签名文件具有以下两个优点:

A signature file has a couple advantages:


  • 您可以在文件有效期内将实体公开,但随后将 private 到项目的后续文件。

  • 您可以在签名文件中仅包含简短摘要,因此易于使用公共界面无需扫描大量代码即可阅读。

  • You can make entities public for the duration of the file, but then private to the subsequent files of the project.
  • You can have just your short summary in the signature file, so the public interface is easy to read without having to scan tons of code.

第一个项目符号不可小视-像这样的内部封装实际上是对于大型项目而言,这是一个相当大的功能。能够定义一些在File1.fs中彼此公共的类型,但是只有一部分这些类型/方法对其他类型(File2.fs,File3.fs等)是公共的(有点像C ++中的朋友。

The first bullet is not to be trifled with - within-assembly encapsulation like this is actually a pretty huge feature for very large projects. Being able to define a few types which are public to one another within File1.fs, but then only have a subset of those types/methods be public to the rest (File2.fs, File3.fs, etc.) is quite useful (a little bit like 'friend' in C++).

这篇关于F#中的签名文件和访问修饰符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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