I just read the information on this page, and while a new ? operator is mentioned, it's quite unclear to me what would its usage be.
Could anyone please provide a quick explanation, post a code snipped of how would this operator be used and possibly mention a use case?
Edit: this is really awkward, I've noticed that the ? operator is no longer mentioned in Don's release notes. Any idea of why is that?
相关问题
- Visual Studio 2010 randomly says the command line
- F#: Storing and mapping a list of functions
- (ASP.NET) Project file must include 'WindowsBa
- Partial Form Class C# - Only display code view for
- Why does this code not compile?
相关文章
- How to show location of errors, references to memb
- Log4Net Multiple Projects
- FSharp.Data.JsonProvider - Getting json from types
- Compiling error in C++ project with C and C++ code
- Signing an F# Assembly (Strong name component)
- How to use Mercurial from Visual Studio 2010?
- VSIX: execute code on VS startup
- Learning F#: What books using other programming la
There are two new "special" operators in this F# release, (?) and (?<-). They are not defined, but they are available for overloading, so you can define them yourself. The special bit is how they treat their 2nd operand: they require it to be a valid F# identifier, but pass it to function implementing the operator as a string. In other words:
is desugared to:
and:
is desugared to:
A very simple definition of those operators could be:
Note that since the return type for the gettor is generic, you'll have to specify it at use site in most cases, i.e.:
though you can still chain-call (?) (since first argument of (?) is also generic):
Another, more interesting, implementation is to re-use CallByName method provided by VB:
The advantage of that is that it will handle both properties and fields correctly, work with IDispatch COM objects, etc.
There is a module FSharp.Interop.Dynamic, on nuget that implements the dynamic operator using the dlr.
It's open source, Apache license, you can look at the implementation and it includes unit test example cases.
It sounds like the "?" operator relates to the Dynamic Language Runtime (DLR). That is, you use it when you want to bind to an object member (method, property) at runtime, rather than at compile time.
It's funny because I was hoping that this would be how dynamic member invocation would work in C# also. Alas, C# exposes this functionality via a "pseudo" type ("dynamic" IIRC). In my opinion, this makes the code somewhat less clear (because you have to track down the variable declaration to know if the call is early-bound or late-bound).
I don't know the exact syntax, but if I had to guess, it either replaces or augments the "." (dot) operator. As in:
or maybe: