F# Type Providers in Depth
-
Upload
tomas-petricek -
Category
Technology
-
view
3.813 -
download
0
description
Transcript of F# Type Providers in Depth
![Page 1: F# Type Providers in Depth](https://reader033.fdocuments.in/reader033/viewer/2022052904/557cf415d8b42a57118b479d/html5/thumbnails/1.jpg)
F# Type Providers in Depth
Tomas Petricek@tomaspetricek | http://tomasp.net
![Page 2: F# Type Providers in Depth](https://reader033.fdocuments.in/reader033/viewer/2022052904/557cf415d8b42a57118b479d/html5/thumbnails/2.jpg)
![Page 3: F# Type Providers in Depth](https://reader033.fdocuments.in/reader033/viewer/2022052904/557cf415d8b42a57118b479d/html5/thumbnails/3.jpg)
F# 3.0 in Visual Studio 11
![Page 4: F# Type Providers in Depth](https://reader033.fdocuments.in/reader033/viewer/2022052904/557cf415d8b42a57118b479d/html5/thumbnails/4.jpg)
The Problem
<s:complexType name="Session"><s:sequence> <s:element minOccurs="0" maxOccurs="1" name="CustomerID" type="s:string" /> <s:element minOccurs="0" maxOccurs="1" name="CustomerName" type="s:string" /> <s:element minOccurs="0" maxOccurs="1" name="BranchNumber" type="s:string" /> <s:element minOccurs="0" maxOccurs="1" name="SessionKey" type="s:string" /> <s:element minOccurs="0" maxOccurs="1" name="ChosenDeliverySlotInfo" type="s:string" /> <s:element minOccurs="0" maxOccurs="1" name="CustomerMessageOfTheDay" type="s:string" /> <s:element minOccurs="0" maxOccurs="1" name="CustomerForename" type="s:string" /></s:sequence></s:complexType>
type Session = { CustomerID : int CustomerName : string BranchNumber : int SessionKey : string ChosenDeliverySlotInfo : string CustomerMessageOfTheDay : string CustomerForName : string }
Meta-data information
≠F# type
declaration
![Page 5: F# Type Providers in Depth](https://reader033.fdocuments.in/reader033/viewer/2022052904/557cf415d8b42a57118b479d/html5/thumbnails/5.jpg)
The Problem
![Page 6: F# Type Providers in Depth](https://reader033.fdocuments.in/reader033/viewer/2022052904/557cf415d8b42a57118b479d/html5/thumbnails/6.jpg)
Levels of Data Access
Expression ScaleDynamic typing
Program ScaleCode generation
Internet ScaleType providers
![Page 7: F# Type Providers in Depth](https://reader033.fdocuments.in/reader033/viewer/2022052904/557cf415d8b42a57118b479d/html5/thumbnails/7.jpg)
Type providers change how you think about
programming!
![Page 8: F# Type Providers in Depth](https://reader033.fdocuments.in/reader033/viewer/2022052904/557cf415d8b42a57118b479d/html5/thumbnails/8.jpg)
![Page 9: F# Type Providers in Depth](https://reader033.fdocuments.in/reader033/viewer/2022052904/557cf415d8b42a57118b479d/html5/thumbnails/9.jpg)
DEMO: WORLD BANK
![Page 10: F# Type Providers in Depth](https://reader033.fdocuments.in/reader033/viewer/2022052904/557cf415d8b42a57118b479d/html5/thumbnails/10.jpg)
What is a Type Provider?
![Page 11: F# Type Providers in Depth](https://reader033.fdocuments.in/reader033/viewer/2022052904/557cf415d8b42a57118b479d/html5/thumbnails/11.jpg)
How are Type Providers used?
Type provider
IDE
IntelliSense for Generated Types
Compiler
Type-Check Imported Types
Compile using Type Provider
![Page 12: F# Type Providers in Depth](https://reader033.fdocuments.in/reader033/viewer/2022052904/557cf415d8b42a57118b479d/html5/thumbnails/12.jpg)
Type Providers for .NET
Hiding code generationWeb Services (WSDL)SQL databases (LINQ to Entities)
Erasing provided typesREST services (World Bank)Schematized web (Freebase)
![Page 13: F# Type Providers in Depth](https://reader033.fdocuments.in/reader033/viewer/2022052904/557cf415d8b42a57118b479d/html5/thumbnails/13.jpg)
DEMO: WSDL, SQL, ODATA
![Page 14: F# Type Providers in Depth](https://reader033.fdocuments.in/reader033/viewer/2022052904/557cf415d8b42a57118b479d/html5/thumbnails/14.jpg)
Queries in F#
Can be turned to quotations
Extensible query language
query { for movie in netflix.Titles do where (movie.Name.Contains(search)) select movie }
query { for index in Numbers do reverse takeWhile index > 10 }
![Page 15: F# Type Providers in Depth](https://reader033.fdocuments.in/reader033/viewer/2022052904/557cf415d8b42a57118b479d/html5/thumbnails/15.jpg)
Implementing Type Providers
public interface ITypeProvider { Type[] GetTypes();
Expression GetInvokerExpression ( MethodBase method, ParameterExpression[] params );
event EventHandler Invalidate;}
![Page 16: F# Type Providers in Depth](https://reader033.fdocuments.in/reader033/viewer/2022052904/557cf415d8b42a57118b479d/html5/thumbnails/16.jpg)
Design Considerations
Choosing the right viewProvide the right projectionInferring structure from data
Schema structureRefreshing the schemaCaching of online schema
![Page 17: F# Type Providers in Depth](https://reader033.fdocuments.in/reader033/viewer/2022052904/557cf415d8b42a57118b479d/html5/thumbnails/17.jpg)
DEMO: XML TYPE PROVIDER
![Page 18: F# Type Providers in Depth](https://reader033.fdocuments.in/reader033/viewer/2022052904/557cf415d8b42a57118b479d/html5/thumbnails/18.jpg)
Summary
The ease of dynamic languagesGenerate types from schemaNo actual types are needed
With the static typing benefitsChecked at compile-timeFull IntelliSense support
![Page 19: F# Type Providers in Depth](https://reader033.fdocuments.in/reader033/viewer/2022052904/557cf415d8b42a57118b479d/html5/thumbnails/19.jpg)
Compile-Time vs. Run-time
Type Provider
Data source
Executable
Runtime API
Provided code
Generates
Uses Accesses
Accesses
Provider
![Page 20: F# Type Providers in Depth](https://reader033.fdocuments.in/reader033/viewer/2022052904/557cf415d8b42a57118b479d/html5/thumbnails/20.jpg)
Compile-Time vs. Run-time
Type Provider
Executable
Generates
Accesses
ProviderRuntime API
Provided code
Data source
![Page 21: F# Type Providers in Depth](https://reader033.fdocuments.in/reader033/viewer/2022052904/557cf415d8b42a57118b479d/html5/thumbnails/21.jpg)
Structure of a Simple Provider [<TypeProvider>]type SampleTypeProvider(config: TypeProviderConfig) = inherit TypeProviderForNamespaces() // Define new type Samples.GeneratedType let thisAssembly = Assembly.GetExecutingAssembly() let providedType = ProvidedTypeDefinition( ... ) do // Add property 'Hello' that just returns a string ProvidedProperty ( "Hello", typeof<string>, IsStatic = true, GetterCode = fun args -> <@@ Runtime.lookup "Hello" @@>) |> providedType.AddMember // Register the type with the compiler this.AddNamespace(namespaceName, [ providedType ])