📜 ⬆️ ⬇️

Innovations F # 3.0

At the recent Build conference, in addition to the already widely covered and discussed presentation of Windows 8, Metro UI and WinRT, there were still a lot of interesting things. In particular, Don Syme and his team presented a developer preview of a new, third, version of the F # programming language, which is part of the developer preview Visual Studio 11 (and, by the way, you can already be tested by reference ).


F # (as well as many other functional languages) is a rather powerful, concise and expressive programming language and often makes it possible to solve complex problems using a rather simple and short code. It has already found application in some areas, including financial programs, programs for trading, data analysis, research and others. And although, of course, in terms of popularity, he lags far behind his older brother C # (and I'm afraid, and from the younger one - VB), but he still got some distribution, and he is not going to die yet.

The new version of the language is a logical and consistent development in the direction of even greater brevity and declarativeness. Among the main innovations of the language are LINQ queries, Type Providers, as well as a set of built-in type providers for currently available enterprises and web-based data delivery standards. All these innovations serve one purpose - to simplify work with data and their accessibility. They are part of a new concept that language developers called Information Rich Programming (IRP) . Let us examine these innovations in more detail, and see how they work together.
')

Type Providers


The Type Providers mechanism is a key part of the IRP concept.
One of the most important conditions on the way to the IRP is that language developers see the need to present information from external sources in the form of types, properties, and methods for use in a programming language environment. Writing these types of hands takes a lot of time, effort, and then all this is difficult to maintain. One of the usual alternatives is the use of code generators, which add the necessary files to the project. However, this does not correspond to the research programming methods that F # supports, because with each new connection to the data service it is necessary to change the generated files.

It is here that a type provider, a component that includes new types in the program and methods based on data schemas from external data sources, comes to the rescue. Types provided by type providers are usually based on external information sources. For example, the type provider for SQL already built into the F # distribution will provide types and methods that will allow the programmer to work directly with the tables of any SQL database. The type provider for WSDL works in a similar way.

The types and methods provided by the provider may depend on the parameters defined in the code or configuration files. For example, the provider type may provide different types depending on the connection string to the database or the URL of the web service. Also, a type provider can provide and disclose various data groups only on demand, i.e. they are only revealed if these types are currently required by your program, which allows you to integrate large amounts of data, such as online stores, into the program.
In addition to the already mentioned providers of types for SQL and WSDL, the provider for OData is also integrated. In addition, the programmer can write his own type provider.
Now let's see how this works, using the example of the built-in type provider for SQL:


The Generate attribute defines the type provider class. We only need to register the initialization string, get the data context, and voila, in the next line of our script (note, this is the fsx script file) we have access to the database tables, including with IntelliSense support.

As you can see, all this is really very simple and convenient. But for SQL, there were already so many ORMs and everything else that perhaps this is not amazing. Then take a look at the provider for accessing OData. For example, it is known that ebay allows you to access your lots using OData. Let's connect to this service and make sure everything works.

As you can see, everything is fine, IntelliSense in the script file automatically picked up the store data structure, and everything is ready to use this data.

LINQ Queries


So, we have strictly typed software mappings of external data stores, and that's fine. But now we would like to easily and easily sample them. How? Of course, using LINQ, which became the second F # innovation that supports the IRP concept.
It is implemented using Query expressions, a new kind of computation expression with the keyword query. (If you have forgotten what computational expressions are, they are computation expressions, welcome here , if you are too lazy, then I’ll just say that this is essentially the implementation of monads in F # with all the Bind'ami and Do'mi due).

By syntax, these queries are similar to their C # counterparts, with the keywords select, where, count, etc. (the full list can be found on MSDN). Well and at once a small sample of the code that it became clear at once. Here we are, for the sake of interest, connect to the OData provider from stackoverflow.com:



As a result of running this script in F # Interactive, we get the following result:



Naturally, requests can be of almost unlimited complexity and depth.

In my opinion, a bunch of Type Providers + Queries is quite curious and, possibly, “will take off.” We will wait soon to replenish the collection of providers from third-party developers (for example, it would be interesting to look at the provider for NoSQL). Let's see where the announced IRP-way of developers will lead further.

I also note that in addition to the two main innovations, there will be some minor improvements, for example, the introduction of auto-generated properties, as in C #:
type MyClass(property : int) =
member val AutoProperty = property with get, set



And finally, we are waiting for significant improvements in the integration of F # with Visual Studio, including improved IntelliSense (do you remember VS release without this killer-feature “Improved IntelliSense”? :)) search, hints and warnings.

Thanks for attention!

Source: https://habr.com/ru/post/129043/


All Articles