F#中的AutoOpen属性 [英] AutoOpen attribute in F#

查看:166
本文介绍了F#中的AutoOpen属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用AutoOpen属性的建议原则是什么?

What is the recommended doctrine for using the AutoOpen attribute ?

(此问题可能是在类似地使用模块函数VS静态函数时的延续.命名类型)

(This question is probably is in continuation to when to use module function VS static function on similarly named type )

F#专家指出:在定义临时顶级运算符时,此功能非常有用 和功能:"

Expert F# states that "This can be useful when you’re defining ad hoc top-level operators and functions:"

因此,当您在技术上需要编写代码但从客户端角度删除代码的存在时,这似乎可以减少代码组织中的模块角色.

So this seems to be here to kind of diminish the module role in the code organization, when you technically need one to write the code but you remove its existence from a client perspective.

还有别的吗? 您何时使用它?

Is there something else ? When do you use it ?

推荐答案

我认为AutoOpen属性的主要用途是当您希望当库的用户打开命名空间时使一些让约束值可用时.这是该属性非常有用的地方,因为我认为库通常应该导出命名空间中的所有定义,但是出于某些目的,您需要导出 values ,并且不能在内部定义值一个命名空间.

I think the main use for the AutoOpen attribute is when you want to make some let-bound values available when the user of your library opens a namespace. This is where the attribute is very useful, because I think libraries should generally export all definitions in namespaces, but for some purposes you need to export values and values cannot be defined inside a namespace.

以下是 F#异步扩展中的示例,该示例定义了计算构建器,因此需要导出asyncSeq值(但同时,所有功能都包装在名称空间中):

Here is an example from F# async extensions which defines a computation builder and thus it needs to export asyncSeq value (but at the same time, all functionality is wrapped in a namespace):

namespace FSharp.Async

type AsyncSeq<'T> = (* ... *)
type AsyncSeqBuilder() = (* ... *)

[<AutoOpen>]
module GlobalValues = 
  let asyncSeq = AsyncSeqBuilder()

该库的用户只需编写open FSharp.Async,他们就会看到asyncSeq.我认为各种数学库(您还希望导出简单命名的函数)都使用相同的模式.

The user of the library can just write open FSharp.Async and they will see asyncSeq. I think the same pattern is used with various math libraries (where you also want to export simple-named functions.)

对于模块(例如ListSeq),我认为大多数人不使用open并通过模块名称(例如List.map)访问功能,因此尽管您可以使用此功能对于嵌套模块,我没有那么频繁地看到它.

For modules (e.g. List and Seq), I think most of the people do not use open and access the functions via a module name (such as List.map), so although you can use this for nested modules, I have not seen that as frequently.

这篇关于F#中的AutoOpen属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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