ENTERPRISE

Windows 8 Architecture from a Developer’s Point of View : Understanding Windows Runtime (part 4) - Language Projections

2/26/2014 2:51:35 AM

3. Language Projections

Programming languages have their own characteristics. Fans of a particular programming language like to work with the language because of its attributes, such as readability, high-performance constructs, functionality, low syntax noise, and so on. When creating Windows 8 style applications, you can use several languages — with different syntax and semantic approaches. However, it was a challenge for Windows Runtime designers to allow using all services from these very different languages.

The Language Projections layer is responsible for transforming Windows Runtime APIs to the shape of the language. Because of this transformation, programmers can use the APIs as if those were the part of the language’s native runtime libraries.

The best way to understand what the Language Projection layer does is to take a look at a simple example. The Windows.Storage.Pickers namespace contains a type named FileOpenPicker that enables you to select a file from a folder. Using the C# programming language, you can use the following code to configure this dialog box for picking up picture files:

using Windows.Storage.Pickers;
// ...
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
openPicker.FileTypeFilter.Add(".jpg");
openPicker.FileTypeFilter.Add(".jpeg");
openPicker.FileTypeFilter.Add(".png");

The same task can be rewritten using JavaScript, as shown in this code snippet:

var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
openPicker.viewMode = Windows.Storage.Pickers.PickerViewMode.thumbnail;
openPicker.suggestedStartLocation =
Windows.Storage.Pickers.PickerLocationId.picturesLibrary;
openPicker.fileTypeFilter.replaceAll([".png", ".jpg", ".jpeg"]);

The identifiers highlighted in bold represent Windows Runtime API types. All of them start with an uppercase letter and use Pascal case, both in C# and JavaScript. The identifiers with italic typeface are members of Windows Runtime types. As you can see, they use Pascal case in C# and Camel case in JavaScript. This is done by the Language Projection layer! Whereas the C# language projection renders member names with Pascal case, the JavaScript language projection uses Camel case — according to JavaScript conventions.


NOTE Using Pascal case and Camel case means writing identifiers in which words are joined without spaces, with each element’s initial letter capitalized within the compound, respectively. Whereas Pascal case uses uppercase for the first letter (such as “P” in “PicturesLibrary”), Camel case has a lowercase letter (such as “v” in “viewMode”).

The task of the Language Projection layer does not stop at syntax sugar. In Windows Runtime API, the FileFilterType property of the FileOpenPicker type is a vector (or array) of string elements (FileExtensionVector). The C# language projection renders this property as a List<string> (list of strings), and so the file extensions can be added with the Add method of the List<string> type, as shown here:

openPicker.FileTypeFilter.Add(".jpg");
openPicker.FileTypeFilter.Add(".jpeg");
openPicker.FileTypeFilter.Add(".png");

The JavaScript language projection renders the fileFilterType property to an array of strings, and so the replaceAll function can be used to set up the content of the array, as shown here:

openPicker.fileTypeFilter.replaceAll([".png", ".jpg", ".jpeg"]);

Visual Studio also leverages the features of the Language Projection layer. As shown in Figure 7 and Figure 8, respectively, IntelliSense in C# and in JavaScript proffers the appropriate continuations, depending on the language.

Figure 7: IntelliSense proffers members of the List<string> type in C#

c03f010.tif

Figure 8: IntelliSense proffers array operations in JavaScript

c03f011.tif

With language projection, you do not need to utilize different tools to access and seize the operating system services depending on the programming language. You perceive all Windows Runtime services as if those were the part of the chosen programming language’s runtime environment.

Other  
  •  Windows 8 Architecture from a Developer’s Point of View : Windows 8 Development Architecture
  •  Windows 7 : Programming KMDF Hardware Driver - Mapping Resources - Code to Map Resources
  •  Windows 7 : Programming KMDF Hardware Driver - Handling Interrupts (part 2) - Deferred Processing for Interrupts
  •  Windows 7 : Programming KMDF Hardware Driver - Handling Interrupts (part 1) - Code for EvtInterruptIsr Callback
  •  Windows 7 : Programming KMDF Hardware Driver - Support Device Interrupts (part 2) - Post-Interrupt Enable and Pre-Interrupt Disable Processing
  •  Windows 7 : Programming KMDF Hardware Driver - Support Device Interrupts (part 1) - Creating an Interrupt Object,Enabling and Disabling Interrupts
  •  Microsoft Exchange Server 2010 : Managing Data and Database Availability Groups - Content Indexing
  •  Microsoft Exchange Server 2010 : Creating and Managing Database Availability Groups (part 5) - Switching over Servers and Databases
  •  Microsoft Exchange Server 2010 : Creating and Managing Database Availability Groups (part 4) - Configuring Database Availability Group Properties
  •  Microsoft Exchange Server 2010 : Creating and Managing Database Availability Groups (part 3) - Managing Database Availability Group Networks
  •  
    Top 10
    Review : Sigma 24mm f/1.4 DG HSM Art
    Review : Canon EF11-24mm f/4L USM
    Review : Creative Sound Blaster Roar 2
    Review : Philips Fidelio M2L
    Review : Alienware 17 - Dell's Alienware laptops
    Review Smartwatch : Wellograph
    Review : Xiaomi Redmi 2
    Extending LINQ to Objects : Writing a Single Element Operator (part 2) - Building the RandomElement Operator
    Extending LINQ to Objects : Writing a Single Element Operator (part 1) - Building Our Own Last Operator
    3 Tips for Maintaining Your Cell Phone Battery (part 2) - Discharge Smart, Use Smart
    REVIEW
    - First look: Apple Watch

    - 3 Tips for Maintaining Your Cell Phone Battery (part 1)

    - 3 Tips for Maintaining Your Cell Phone Battery (part 2)
    VIDEO TUTORIAL
    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
    Popular Tags
    Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8