F#:有没有办法扩展 monad 关键字列表?
在 F# monad 中,如果您说 let!
,编译器会将其转换为您在 monad 构建器上定义的 Bind
成员。
现在我看到有查询单子,如 所示在 MSDN 上,您可以在其中说:
query {
for student in db.Student do
select student
count
}
select
和 count
例如,将被翻译为 QueryBuilder
成员<一href="https://msdn.microsoft.com/visualfsharpdocs/conceptual/querybuilder.select%5B%27t%2C%27q%2C%27result%5D-method-%5Bfsharp%5D" rel="noreferrer">Linq。 QueryBuilder.Select 和 Linq.QueryBuilder .计数
。
我的问题是,关键字到成员的映射是硬连线到 F# 编译器中的,还是可扩展的?例如,我可以这样说:
FooMonadBuilder() {
bar
}
并以某种方式告诉 F# 编译器 bar
映射到 FooMonadBuilder.Bar()
方法吗?
Inside an F# monad, if you say let!
, the compiler translates that to a Bind
member that you've defined on the monad builder.
Now I see there are Query monads, as shown here on MSDN, where you can say:
query {
for student in db.Student do
select student
count
}
and the select
and count
, for example, will be translated to the QueryBuilder
members Linq.QueryBuilder.Select and Linq.QueryBuilder.Count
.
My question is, is this mapping of keywords to members hardwired into the F# compiler, or is it extensible? For example, can I say something like:
FooMonadBuilder() {
bar
}
and somehow tell the F# compiler that bar
maps to a FooMonadBuilder.Bar()
method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 F# 2.0(即 Visual Studio 2010)中,无法扩展关键字列表(Ramon 的扩展除外)。但是,F# 3.0 (Visual Sutdio 11) 中的查询机制是可扩展的,您可以定义自己的关键字,类似于
select
和count
。下面是一个基本示例,它使用
reverse
关键字定义了诸如seq
构建器之类的内容:其工作原理的详细信息尚未记录,但有
CustomOperation
属性表示该操作应被视为特殊语法(您可以设置各种属性来指定其行为方式 -MaintainsVariableSpace
意味着它不会更改序列内的值)。Projectionparameter
属性指定关键字后面的表达式应隐式转换为函数。现在,
mseq
构建器支持select
和reverse
:In F# 2.0 (that is Visual Studio 2010), there is no way to extend the keyword list (other than Ramon's extension). However, the query mechanism in F# 3.0 (Visual Sutdio 11) is extensible and you can define your own keywords similar to
select
andcount
.Here is a basic example that defines something like
seq
builder withreverse
keyword:The details how this works are not yet documented, but the
CustomOperation
attribute says that the operation should be treated as a special syntax (you can set various properties to specify how it behaves -MaintainsVariableSpace
means that it does not change the values inside sequence). TheProjectionparameter
attribute specifies that the expression following the keyword should be implicitly converted to a function.Now, the
mseq
builder supports bothselect
andreverse
:简短的回答:不。
我已经扩展了编译器来支持这一点,欢迎您阅读我的博客文章 http://ramon.org.il/wp/2011/04/take-computation-expressions-one-step-further/
Short answer: no.
I've extended the compiler to support that, you're welcome to read my blog article http://ramon.org.il/wp/2011/04/taking-computation-expressions-one-step-further/