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.
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.