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?
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
selectandcount.Here is a basic example that defines something like
seqbuilder withreversekeyword:The details how this works are not yet documented, but the
CustomOperationattribute says that the operation should be treated as a special syntax (you can set various properties to specify how it behaves -MaintainsVariableSpacemeans that it does not change the values inside sequence). TheProjectionparameterattribute specifies that the expression following the keyword should be implicitly converted to a function.Now, the
mseqbuilder supports bothselectandreverse: