The Fundamentals of the Dynamic Language Runtime (DLR)

This high-level introduction to Dynamic Language Runtime (DLR) provides definitions and basics to get you started. This web seminar also demos scenarios involving dynamic languages and .NET and shows advantages of dynamic languages such as IronRuby and IronPython.

Download

Video Transcript

Hello, I’m Nancy Strickland from IT Mentors. My topic today is the Dynamic Language Runtime, also known as the DLR. After a general introduction to dynamic languages in the DLR, I’ll talk about what you have to download and install in order to use dynamic languages with .NET. Then, we’ll look at two of the new .NET Dynamic Languages, IronPython and IronRuby and we’ll work with some of the basic syntax using the command line interface. Then we’ll talk about the role of dynamic languages in Silverlight and we’ll use three tools for creating, editing and testing web applications that use dynamic languages: Chiron, the DLR console and Visual Studio 2008.

Introduction and Installation

So, let’s start by explaining what dynamic languages are and how to install them. Dynamic Languages is a category of languages that execute many of their important features at runtime, rather than at compilation. All dynamic languages do not necessarily have the same features, and some of the features of dynamic languages are also available in standard languages. So, what makes a language dynamic is whether it’s designed to use these features easily and as a basic part of its functionality. So, for example dynamic languages typically don’t require that variables be typed in the source code. The compiler doesn’t check typing, so the type with the variable isn’t determined until the program runs. Now, you know that standard languages also have late binding features. So, this is an example of how the difference between standard languages and dynamic languages is mostly in how they behave in general, rather than any particular feature existing or not existing.

Another important feature of dynamic languages is that new executable code can be loaded at runtime. Obviously this code can’t have been compiled, so it’s interpreted dynamically. These features and others make dynamic languages relatively flexible, so they’re often used for scripting. But they’re not simplistic. They’re full featured, high level and they can be object oriented. Some well known dynamic languages are PERL and Tickle, along with Python and Ruby, which are the languages that we’re gonna be talking about in this session.

The main reason that dynamic languages are popular is that they’re very flexible and programmers often find that their more productive using them. You can write more code faster in part because you have to spend less time writing routine code like declarations because the language isn’t typed. This also makes the code itself shorter and clearer. Dynamic language programmers often say that they feel freer to concentrate on the logic and the algorithm rather than becoming bogged down and writing extra code not directly related to the logical solution to the problem. For example, programmers can simply manipulate variables intuitively without having to worry about extra code to handle casting or type checking.

Another advantage to using a dynamic language is that in general changes to an existing program can be made very simply by just changing the source code. A formal recompilation isn’t needed, which can be a great time and effort saver. And this also means that it’s relatively easy to do iterative development in dynamic languages. You can code a little, run to tests, change the code, run again immediately without spending time compiling or linking or loading. It’s much easier in a dynamic language to develop a little bit at a time because testing is so quick and easy. Many dynamic languages including those that we’re gonna be looking at here make it even easier by providing an interactive console so that you can see the result of each code change immediately on screen in the same console with the editor without even having to do a context switch to run your code.

But nothing is free. The most commonly mentioned disadvantage of dynamic languages is that they run relatively slowly. Now, this is in part unavoidable because some of the work that other languages do at compile time, like type checking ends up being done at runtime. And this is obviously a really serious problem for certain kinds of applications. It means that dynamic languages can’t really be used for applications where speed is the most critical thing. But dynamic languages are most often used in a situation where that’s not true, such as when the execution’s going to be limited by IO speed anyway.

Another thing to consider when you choose a dynamic language is that while it makes testing easier and faster like we just said, it will probably require more testing. For one thing, the compiler isn’t going to help you by doing some of the checking for you. For example, to create a new variable in most dynamic languages you just assign to it. That’s easy, but it can lead to a problem. If you assign 3 to X then a variable named X is automatically created. But if then later you want to assign 4 to X and you accidently type a capital X instead of small, then instead of assigning 4 to lowercase X you’re creating a new variable named upper case X, assuming you’re using a case sensitive language. But other typos work the same way in a dynamic language that’s not case sensitive. So, these kinds of errors have to be found by testing in dynamic languages. In a type language, of course, the compiler would let you know.

So, where are dynamic languages used? Well of course almost anywhere. They’re full featured and they could hypothetically do anything, assuming that execution speed isn’t critical. But one place where they’re really useful is where runtime modifications are frequent and you want to avoid the overhead of frequent recompilation and redeployment. So, for example suppose you have a shopping application and the pricing rules change frequently. You’ve got a sale or promotion, say anything red is 10 percent off in February, or 3 items with yellow tags gets you a fourth item free this Saturday only. Or buy one get one free with a coupon from this week’s paper. Or 10 percent off everything from 5:00 a.m. to 10:00 a.m. the day after Thanksgiving. These are all rules that are short-term and they keep changing. To implement them in a regular language, you’d change your code, you’d recompile and you’d redeploy. This could also require a restart if it’s a web application. But if you’re using dynamic code, you could just change the code and save a new code. The changes take effect immediately. No recompiling, redeployment or restart.

Which brings us to the dynamic language runtime, the DLR. What the DLR does is makes it possible to use dynamic languages with the CLR. The DLR is a set of services that dynamic languages need and it runs on top of the CLR, the common language runtime that .NET uses. So, you can get the dynamic services you need along with the standard CLR services. Now, just like any of several .NET languages can be CLR compliant and use CLR services, several different languages can work with the DLR. Right now, those languages are IronPython, IronRuby and Dynamic Java Script, which is a new implementation of java script for the DLR. This session is going to focus on IronPython and IronRuby.

And the DLR is also multi-platform. There’s a version up and running on Mono which you may already know is an open source version of the common language infrastructure that can be run on Linux, BSD, Unix, Mac OS 10 and Solaris. And it’s multi-browser. Firefox has announced that it will support scripting with IronPython and IronRuby in a project called Iron Monkey. And of course IE already supports both.

IronPython is an implementation of the Python language for use with the DLR. Microsoft has given it to Codeplex and you can download it from there packaged with a tutorial and samples. When this recording was made, the most recent stable release was 1.1.2. But at the Codeplex site, you’ll also see a version 2, but this version is still under development and right now it only works with the community technology preview of Visual Studio 10.

The other language we’ll be using in this session is IronRuby which is an implementation of the Ruby language for the DLR. Now the implementation of IronRuby is not as far along as IronPython, it’s still in alpha. But you can download it from a website, rubyforge.org and use it now. And if you’re already a fan of Ruby, you can be involved in the development of IronRuby. You can get more information at the IronRuby net site.

So, in order to run either of these languages or to copy the demos that we’re going to do in this session, you’ll need to download either IronPython or IronRuby or both because I’m going to be demoing both. You’ll also need to have Vista SP1 and the .NET framework 3.5 SP1. Earlier versions of IronPython can work with XP, but there’s been significant changes in the language so most of the demos I’m going to do won’t work with those versions.

To install the languages, just download the zipped installation files shown here from the location shown here, and unzip them into a folder of your choice. To make your life easier, you’re also gonna wanna add these folders to your path environmental variable. And again, let me warn you that both IronPython and particularly IronRuby are relatively new languages. They’re still being developed. And from one version to another there’ve been quite a few breaking changes. So, you’ll have to have exactly the same environment and versions mentioned here to do the demos that I’m going to show. And there is no guarantee that the next release, again particularly in the case of IronRuby that’s still in alpha, won’t break some of what I’m going to show you.

So, the purpose of these demos and of this session in general is not really to teach specifics that are guaranteed to be stable for years. It’s really so that you can become familiar with what dynamic languages are and what they can do and how they’re implemented on the .NET DLR in a general sense. And the basic syntax of IronPython and IronRuby is not likely to change much since they’re based on the existing Python and Ruby languages. What may be continuing to change and evolve is how you work with the DLR, so just be aware of that as we go along. Also, if you see tutorials or samples in other places and it’s different from the code you see here, or if you try to use code from here or from somewhere else and it doesn’t work, the first thing to check is whether all of the versions you’re using match the versions that they’re using.

The first demo here is just showing the basic installation of the language, which is very straightforward. I’ve downloaded the current versions of IronPython right here, IronPython 1.2.2 and IronRuby as zip files. I’m now going to unzip them right at the root, although you might choose to unzip them in the program files folder or somewhere else. So, I’ll have a folder named IronPython and one named IronRuby.

Now, I’m going to set my path so I can access the executables for these languages easily. In advanced system settings, I’ll select my environmental variables and change my path variable. The path to IronRuby is easy, it’s just c:\ironruby, because I put it there at the root. But I do have to include the bin directory. So, it’s c:\ironruby\bin. And IronPython is ironpython-1.1.2. That’s really all there is to it for the installation. So, I’m going to finish up this demo by showing you a little bit of what a dynamic language can do in a web application uses IronRuby, combined with C# on top of the DLR on top of the CLR. It’s an app that searches Flicker for images and displays them. Now, I’m running it locally here with a little utility that conserve pages. We’ll see how to use that later.

This is an application that searches Flicker for images and displays them. As you can see I’ve downloaded it and I’m running it locally here with a little utility that conserve pages. We’ll see how to use it later. And this demo actually I’ll show you how you can get it yourself later as well. So, I’ll just type in something and see what I get. And then I can click on any particular image here to enlarge it. And I’ve also got a link to Flicker over here if I want to go see the original page. Just a pretty little demo. You can get it included with the software development kit for using the DLR languages with Silverlight. It’s called the SDLSDK. We’ll use it later in this video and I’ll give you the URL for it then.

Command line IronPython and IronRuby

Now, let’s start learning dynamic net languages. I’ve got the languages installed, so let’s look at some basic syntax and see how to write and run simple programs. We’ll start with IronPython. We’ll use an interactive command line interface to learn the basics of the syntax. To start an interactive command line session you run ipy.exe. It’s located in the IronPython folder and since we’ve put the folder with this executable in the path, you should be able to run it from anywhere, just type IPY. You’ll see when it starts up that you get a prompt with 3 angle brackets and here you can type commands and they’ll execute immediately. To exit you type CTRL+Z and return.

To write the standard hello world, you just type print and then in quotes “hello world.” You’ll need quotes and these could have been single quotes here as well as doubles, so that you’ll have a single string argument to the print command. Notice that IronPython is case sensitive so print has to be all lowercase. The next statement here doesn’t even have a print statement, it’s just an expression. When IronPython sees an expression, it evaluates it and returns the result so the output here is 4.

Now, instead of interactive input, we can also create a simple program. You just use any text editor, Notepad’ll do, to write the code and then you execute it with the IPY command. So, here I’ve typed the program hello.py. Notice that the standard extension for IronPython is PY although it’s not strictly required if you’re using IPY at the command line to run the file. So, the contents of that file is print hello world, there it is with the single quotes. And then when I type IPY, hello.py, so I’m taking hello.py and using it as the input to be run with the interactive IronPython program, then the output is hello world.

You probably won’t be doing much input from the command line, but a quick demo here shows you how typing works in IronPython. The raw input method, accepts user input with a string prompt that is an optional argument to the method. And it returns it as a string. Notice that I didn’t declare name at all. I just used it as the target of an assignment and it’s created automatically. Because the raw input method returns a string, then it runtime name is dynamically typed as a string.

Defining a function in IronPython requires the keyword depth, the name of the function, separated arguments of any in parenthesis and that’s followed by a colon. The lines of the function, each line must be indented. In Python, indenting defines a code block, not begin, end or curly braces or anything else. The function then uses the return keyword if it needs to return a value. So, here calling the hello function in the print line returns hello bob.

What we’ve seen so far is pretty much standard Python. What IronPython adds is the ability to interact with the CLR via the DLR. So, in this example you see that I can import classes from the CLR. I’m importing SYS here first and then adding the LIB folder from the current directory which is the IronPython directory so that I can access library functions easily during the rest of the program. And then I import the CLR and I use it to add a reference to the forms name space. And then next I import from that name space the application and the form classes. And then I create a new class named hello world form. There’s the class declaration, you can see the colon after it and then the indented lines.

In its initialization method, that is the constructor, I’ll set the text and the name for the form, I’m setting both to hello world. Notice that the constructor is named __init__. The double underscores are a convention for naming a private method within a class. Then I’m going to instantiate a hello world form and assign it to a new variable called form, which at runtime becomes an instance of the hello world form class. Now I’ll write code to make the app start normally without the interactive window by creating an application object and running it with the form. And then the output of running this, the last line there that says IPY win.py, win.py is the name of my program that I wrote, then the output is a little window pops up with the title, the text of hello world.

A pound sign is used for a single line comment and three double quotes together, no spaces, are used to start and end a multi-line comment. Now, let’s see how to do these same things in IronRuby. You start IronRuby the same way. The name of the executable is IR and it’s located in the bin subdirectory of the installation folder. You can exit with CTRL-Z or you can use the word exit. And IronRuby is also case sensitive.

When IronRuby is used interactively, after each statement the result of evaluating that statement is shown. So, if I type 3 x 4, like it shows here, the result of evaluating that expression is shown, 12. If I use the IronRuby version of print, which is put s, then first the output stream is shown, it prints hello world, and then the result of evaluating the expression is shown, which is nil, nothing. And see that although put s is a function with an argument, parentheses aren’t required if you have multiple arguments, just comma separate them. You create an IronRuby text file the same way you do an IronPython, just a simple text file. And then you run it from the command line with IR. The text file extension should be RB.

A difference between IronRuby and IronPython is that when you get user input with the get S command, you’ll get the return character along with it. If you want the string without the return character, you use a method, chomp. This should be familiar to PERL programmers. Again, you can see that the get S method doesn’t have arguments, so no parentheses. So, I put s to ask the user to enter their name, get S to get the name, use the chomp method on name, name.chomp, no parenthesis, assign it back into name. And when I write it out, I say put s, hello, name and then I’ve got the exclamation point there just to show that it’s all happening on the same line.

Even though data’s not typed until runtime, if data is stored as a string at runtime, which is what get S returns, then you can’t do math with it. If you need a string value, you’ll have to use the 2I method on the variable that holds the string to convert it to an integer. So, you can see we have put s enter num. We get S and assign that to num string, because that’s what get S returns is a string. Then we take num string.2I to convert it to an integer and put it in the variable num. Then we can take that variable num and multiply it by 2 and print it out with put s.

Of course there are other methods, string methods like 2F that converts it to a float, or upcase for upper case, or reverse to reverse the characters in the string. There’s lot of other things we haven’t talked about. IronPython, by the way, can’t do math with strings and numbers either, but it has a different input method. It’s named input instead of raw input to get number input from users. We didn’t cover that method before either so I should maybe tell you now that this session covers only a little of IronPython and IronRuby syntax. We’re going to work instead on how you can use the DLR languages and tools in various ways. There are two other videos, one for C# programmers and one for VB programmers that covers syntax for these languages in more depth and they’re available on the ISVInnovation site.

Just like with IronPython, the whole point of IronRuby is to work with the CLR. So, here’s how you can include CLR name spaces in your application using the require keyboard. Notice that you have to provide the fully qualified strong name of the system windows forms namespace. Then you can use the classes and the CLR just as though they were part of IronRuby, there’s no rap or needed. So, here’s we’re using the console and the message box. So, I’ve got system console right line hello world. Notice that in IronRuby when you use methods from these namespaces, the name of the method is the same, but it’s lowercase and underscores are used to separate multiple words. So, the console right line method becomes lowercase right_line and the form show method is lowercase.

Notice that in IronRuby when you use methods for these name spaces, the name of the method is the same, but it’s lowercase and underscores are used to separate multiple words. So, the console right line method becomes lowercase right_line and the message box show method is lowercase. IronRuby functions also start with def, but they use the keyword end. So, the lines inside the function don’t have to be indented. They also don’t require the colon before the body of the function. Also notice that to use the past and variable name I have to put it in curly braces and I proceed it with the # sign.

Now, even though you probably won’t be doing too much work entering data directly from the command line like this, I’m showing you the console, in part so that you can see the message from IronRuby that says that local variables don’t work today in the console. See, that right there after—just before the first prompt? So, this is a reminder that IronRuby is still in alpha and it also says to use global variables instead which require a prefixed $. You might also notice here how multiline commands are handled. After you enter the def line and press return, you get a prompt that’s an ellipses, three dots, and you keep getting this prompt until you type the end keyword. Then the function evaluation, which is nil, is shown and the command ends. And then you get another prompt and at that prompt you can execute the function which will print out hello Bob, in this case, and also of course evaluates to nil. IronRuby has single line comments and they start with the # sign.

Now, let’s do a quick demo of both of these languages from the command line. To do the demo you’ll just need the set up we already have. Vista with .NET 3.5 and the latest IronPython and IronRuby. Let me start by opening a command prompt and then I’ll start IronPython. We put it in the pass so it should start from here and there’s our prompt, the three carrots. Of course we should start with hello world. And for another example of how IronPython evaluates the expression that’s input on the command line I can type 2 + 2 or any math expression. And the output is the result of evaluating that expression. Now, I’m gonna use the exponentiation operator, which is * *. And notice that I don’t have to worry about whether I’m dealing with an integer or a long. Although you can see the L at the end, this is a long.

This is nothing special, its normal behavior for dynamic languages. What’s new is the integration with the .NET framework. So, I’m going to exit with CTRL-Z so I can start IronPython again and this time I’ll add auto complete. And now I’m going to import a CLR namespace. Now, I can use the system class. For example, I’ll type print system and then after the dot, I can use the tab key to scroll through my choices. I’m tabbing here and seeing my choices. I can also speed things up if I want by typing the first letter or two or three letters before I start to scroll so that I get only the E’s here. And finally I’ll get to environment and I’ll have it tell me the current directory. There it is.

So you can see that IronPython can access .NET. I don’t have to have any wrappers, they’re fully integrated, just as though they were classes written in IronPython. I’ll use CTRL-Z to exit again because I want to write a program to execute from the command line. I’m just going to use the old Dos edit command to create a text file named hello PY, Python. And inside it, I’ll just put my print command. I’ll save it, exit back to the command line and run it and there’s my output.

Now, let’s do the same things with IronRuby. I’ll start IronRuby and do a quick interactive test. You see it pause and wait for me interactively to enter something. And then see how the evaluated value is Bob with the new line character, let’s Chomp it. What happened? It doesn’t know what name is. Remember the warning we got earlier about local variables not being supported? Let’s do it again with the global variable. That looks better.

Now, there’s one more thing I want to do and that’s to show you a more complete windows application using WPF instead of Windows Form. I’m going to use IronPython for this so first I wanna show you an existing file named wpf.py. It’s a little utility script that declares two name spaces from WPF. See, first we import the CLR and then we declare our two name spaces or we add references to our two name spaces. And then we define some aliases for some basic classes that you’re likely to wanna use. We got window, application, button, stack, panel, label, thickness, and drop shadow effect. This file is part of the SDL-SDK that I mentioned earlier, the development kit that we’re going to download to work with Silverlight in the next section of this video. The file can be put right into your IronPython folder which we put in a path or you can add its folder to the existing path.

Now I wanna open hello, this is a hello world with WPF, hello.wpf Python and you’ll see that the first line imports everything from our utility script from WPF. That saves repetitive typing and on the second line I can just use window to instantiate a new window and then set its title. I’m also going to instantiate a button and I’ll set its content to click here and then I’ll instantiate a stack panel and I’ll set my windows content to that stack. Then I’m gonna write a method here. Here’s the def click method, sender args and I’ve got a colon there and I’ve got me indented lines. I create a label, just by assigning it to a variable message. Set its content, welcome to IronPython and then add that label to the children collection of my stack.

And then I also add the button to the stack and finally I connect the click method that I wrote to the click event of the button handler. Now all that’s left is to create a new application and have it run the window that I wrote. So, now I’m gonna go to the command line again and I’ll run that application. I’ll need to change to my desktop which is where it is and I’ll run it interactively IPY hello.wpf.py. And I’ve got my little window, I’ve got my button click here and when I click welcome to IronPython.

So, now that I’ve finished the first demonstration to the IronPython and the IronRuby, this might be a good time to comment on the differences between the language, which one should you use. Of course, you haven’t seen a lot of them yet, but let me tell you that they’re both full featured languages and each can really do anything the other can. Right now, IronPython has a big advantage of being farther along in development than IronRuby. IronRuby is still in alpha and there are quite a few things that still need to be straightened out. So, it might be less frustrating for you to start working in IronPython. But if you already know Ruby, then I’d stick with it. It will almost certainly be getting to the release stage pretty soon and you can even participate in the community development effort. And even if you don’t know Ruby, it’s worth learning it because Ruby has some nice advantages itself including the famous Ruby on Rails, which is a popular web application framework being implemented for the DLR. If you don’t know either language, I would suggest you start with IronPython ‘cause it’s simply working better right now, but I’d also suggest you play with Ruby either now or after it becomes more stable and then decide which is your favorite, or for that matter learn both.

Silverlight

Now we can look at Silverlight and see how dynamic languages can be used with it. Although dynamic languages can be used from the command line or to create WPF or win forms or in other ways, they’re likely to be most used with web applications and particularly with Silverlight. So what is Silverlight? It’s a browser plug-in that users can download when they access a website that requires it. As a technology, its purpose is to let you develop RIA, rich internet applications, which means web applications with all the functionality and the aesthetics of desktop based applications. That means it provides audio and graphics including animated graphics and video. The latest version of Silverlight is version 2.0, but that version was first called version 1.1 so that’s a little confusing because now there is no version 1.1, it didn’t ship, just 2.0. So, you’ll have to be careful about possible compatibility issues. The important thing for us to know about this new version is that it supports the DLR, which means we can code for it in IronPython or IronRuby.

You may already be familiar with Silverlight, but let me briefly explain how it works. It’s a web tool so the user interface is laid out in a markup language, but not HTML, it’s called XAML, X-A-M-L, and it includes tags that can work with the code behind page. So, that’s the basic set up, separating the presentation from the code and that’s probably familiar to you. The code behind can be written in any .NET language and as of 2.0 that includes DLR, dynamic languages. Now even though Silverlight code can be written in any .NET language and uses .NET namespaces, it uses a subset of the framework only and of course code for Silverlight can’t make calls to the underlying platform API of the client because that would be a serious security issue, so it’s restricted to a sandbox of functionality.

The combination of the DLR in Silverlight is called DSL, dynamic Silverlight. To use it, you’re going to have to download the SDLSDK, Silverlight Dynamic Languages Software Development Kit. It’ll give you a set of 4 dlls that are needed for the runtime and several others. You’ll also need the dlls for the programming language of your choice and for its libraries. All of these are packaged together in the SDLSDK along with several useful utilities including CHIRON and SL that we’re gonna talk about in just a couple of minutes. And of course you’ll also need to install the actual Silverlight runtime on your client if you don’t have it already.

When you create a Silverlight application you’ll compile an assembly, a dll. That assembly will be combined with any other dlls that your application needs, including the dynamic scripting dlls that we just talked about and any other static resources the app needs. And they’ll all be zipped together into a big file with a XAP, X-A-P, XAP extension. That extension can be processed by Silverlight at runtime. To create the XAP file you’ll need a tool. CHIRON, which is included in the SDK is a basic standalone tool that has a packager to create the XAP file for you. And it has a nice little bonus which is a server that can serve the XAP up in a browser so that you can easily run it during testing while you’re developing. Visual Studio can also be used to create Silverlight project, it has a template for them and when you build their project it will create the XAP file for you.

We’re going to look first at creating a Silverlight DSL project without using Visual Studio. To create a Silverlight app a specific setup of files and folders is required. So, the easiest way to create that is to use a utility named SL-BAD, batch file that’s packaged with the SDK. You can see there’s two similar commands depending on whether you want a Python app or a Ruby app. And you give it a path to the folder for the app and it will create the folder and the application structure inside it.

Here’s part of what the structure of your new Silverlight web folder’s gonna look like. In the top folder is an HTML file named index.html and that’s what hosts the Silverlight. Also in the top folder there’s a subfolder named either Ruby or Python, depending on what you’re coding in, and inside it is a file named either app.rb or app.py. It has to have this name, so don’t change that. It has to be named app and RB or app and PY.

So, you’ve created your folder structure and now you can go in and edit the code in the app file. You can do that just with a text editor or if we were using Visual Studio and Visual Studio, we’ll see that later in the next segment. If you’re just using a text editor, just make your changes and then to test them, make them and save them, and then to test them open a command line prompt, change into the application directory and run CHIRON, that’s the utility we talked about earlier with a /b switch and that means package my app and serve it to me a browser. Your app opens up and you can see the output. Then if you see that you need to make changes, you just edit the source code and refresh the browser, that’s it. This is a dynamic language so no recompiling or deploying is needed. If you don’t need to serve the app you just wanna package it, then this is the command that you use.

So, before I demo how to do this, let me go over the new requirements for working with DSL. You need to have IIS installed locally of course so we can use local host. You’ll need to have Silverlight itself of course, version 2, RTW released to web installed. You’ll also need to download the SDK and you’ll want version 0.4.0, here’s the place to download it from. And that will include the SL bat we talked about and CHIRON, and you’ll want to download the Silverlight toolkit that provides some nice extra controls and components for you.

Silverlight with Chiron Demo

Now let’s see the demo. We’ll start by installing the SDLSDK. I’ve already downloaded it and I’m going to unzip it to a folder right here on the desktop just for the convenience in doing the demos, you’ll probably wanna put it somewhere else. And now I’ll open it. You can see that it has a samples directory here. Then here in the bin directory is the CHIRON utility that I talked about earlier. We’ll get back to it in a minute. And in the script directory, is the SL batch file that I need right now. So, I’m going to go to command prompt to run it. This is going to create a basic structure for a Silverlight app so we can inspect it. I wanna create a Python app and I’ll create it right here on the desktop and call it test.

So, what that’s done is create a directory, a folder named test with the basic skeleton for Silverlight project written in Python. There’s an index.html file. I’m going to open it in notepad so that I can see what’s going on. You can see that it has an object tag here. And inside that object tag is a parameter source and it points to the XAP file, the basic application packaged as a XAP file. Now I’m going to open the Python directory and there are the PY and XAML files. If we look at the XAML, we can see that all that’s there is a text block named message. Now let’s look at application.py, our code behind here, and we can see, it’s a little hard to see because it’s not formatted well, but that its initialization method here, we’re going to set the text part of the message there, that text block to welcome to Python and Silverlight.We can change that to something else, but we won’t right now. Remember this app has to be named app.py or app.rb for Silverlight to find it.

First, we’ll just run the app as it is using CHIRON. I need to have it in the path to do that. So, I’ll edit my path again. There’s my path to my SDLSDK in the bin folder. Now open a prompt and I’m going to change to that test directory and that’s where I’m going to run CHIRON from. Because I use the /b browser option CHIRON not only packages the XAP for me, but it also opens a browser and serves it up to me. And so here’s my index HTML file. I’m going to open it up and here’s our application welcome to Python and Silverlight. It put something into that text block. Of course you can do exactly the same things with Ruby, but it’s so similar I’m not going to repeat it.

Now, suppose we’re developing. I can go to my code here and change it. I could do it in notepad or something else, but I’ll say welcome to ISV Innovation, just right here in notepad. Save it and all I have to do is refresh my browser. I didn’t have to recompile it, I didn’t have to redeploy it, I didn’t have to do a start soft with a server, nothing. Notice that the window that has CHIRON running in here, my little prompt window, stays open. Closing it is gonna stop the server that it’s providing. You can see how handy CHIRON can be then for our code test, code test development cycle.

Now let’s look at another great utility. This is the DLR console which is a great testing and learning environment for IronPython and IronRuby. You can access it online at this URL or it’s part of the SDK, so you can run it locally. To do that navigate to the samples folder that we saw inside the SDK and to the DLR console folder and then run CHIRON just like we did before to open it in a browser because the DLR console is itself a DSL application. There are three modes you can use the tool in, the first is console, it lets you do interactive testing of Python or Ruby commands one statement at a time. So, it’s a lot like running from the command line like we did earlier, except that you can give WPF commands and you’ll immediately see their results in the Silverlight canvas on the right. You’ll also be able to see return values and error message. And there’s also a limited auto complete feature like we saw on our command line example. It doesn’t work in every case of course, because some variable types can’t be determined until runtime so there’s no way to know what method the object and the variable might have.

The second mode lets you experiment with XAML for laying out your GUI. Again, the results show up in the canvas but not interactively, not immediately as you enter tags, you have to render your XAML using CTRL-Enter, but you can do this after each change if you want. The third mode accepts executable Python or Ruby code as a whole program and executes it as a whole when you press CTRL-Enter not line by line as its being entered. So, this lets you test your code behind. To use this, you don’t need anything more than we already have except internet access to the site that hosts the DLR console or since the console is actually itself a DSL application that’s included with the SDLSDK, you might be able to run it locally instead.

So, let’s play with the console. You can see that I’ve opened my browser and I’ve pointed it to the Silverlight site, Silverlight.NET/sample/sl2/dlrconsole. I could also do it, this is just to show you what it looks like, I’m also going to do it from a command prompt, just so you can see how to run it locally. To run it locally, I’m gonna open a command prompt and I’m gonna move to my SDLSDK folder and within that I’ll go to samples and then to my DLR console as a sample app. Then I’m just gonna run CHIRON with my browser option and it opens in a browser. You can see that this is an application like we saw before. I’ll open my index.html and my console will open again, my DLR console for testing. There it is.

I think the best way to demo how the console works is to use the samples provided. We’ll start here with the console tab and you can see down here at the bottom that you can choose to work in Python or Ruby. I have a Python prompt now. If I switch to Ruby, I’ll get a Ruby prompt, so I can work in either one. I’ll start by just using Ruby. The two examples, by the way, the two demos given here are exactly equivalent. So, that’s also gonna give you a chance to compare the two versions.

So, I’m gonna start entering these commands line by line. I’ll first require and I got a fault. Now that means that it wasn’t able to find it. I happen to know why. The WPF utility script is included with the SDLSDK, but it’s capitalized there and Ruby is case sensitive, so I’ll retype it as wpf.rb and be fine. I’ve got the true now. And you’ll notice of course that require doesn’t have a visible affect over here on the canvas, you have to look at the return value.

Now, I’m going to continue to type these commands in, but you’ll see I can’t see the whole next command and that’s one that’s gonna have an affect on the canvas, so I’ll just slide this over so I can see it. The canvas is already provided for us of course, so I just use a canvas background equals. And if I typed it in correctly, I immediately see the change. Nothing visible is gonna happen in the next two lines where I create the text box and set its text.

Now notice that I had to use the $R. Remember that that’s something probably temporary in Ruby. It doesn’t handle local variables yet, so we’re using global and we saw that earlier. You can see that I’m just copying line for line down here from the demo. Nothing’s happened yet, but it will happen when I add my box. There it is. It’s hard to see, but it’s right up there. Let’s make it easier to see, we’ll reset the font. Now we can see it. And we’re gonna also just place it in a different place. And now I’ll add a behavior. I’ll say that when we have the mouse left button down, lost my cursor here, and here is a little, sort of a macro, sort of an included. And now when I go over and click on it, you can see that it says ouch. Now that output because it was a put s is executed in the console window as you’d expect, not on the canvas because we didn’t say to change the canvas in any way.

Now, I’m going to change to Python. Notice that when I change nothing clears. I get a Python prompt here. If I want to clear, I’ll have to refresh. So, I’m gonna do that and do a little Python for a moment. I’ll import WPF and then I’ll add a button. Notice that I have to use the WPF prefix even though I imported it, that’s just how Python works. I’ll use auto complete to help me fit the button’s content. See how it binds it? And there’s my button. Now let’s add an event for it. And notice that after I hit the colon and go to the next line, the prompt changes to a bar to show I’m entering a multi-line statement. I have to indent the next line and I’m gonna just reset that content. And now I’m still showing a bar, so on this empty line, this blank line, I’m gonna hit enter again and my prompt changes back, it knows that I’m finished entering the content of my function definition. Now, I’ll wire up the click event of the button to my hander, which is also called click. My button has appeared over here and I click it it says okay, so it works.

Now let’s look at the XAML tab. I’ll just quickly add a button here. And to render it to the canvas, I’ll have to type CTRL-Enter and there’s my click button. I’m not gonna spend too much time here because typing in XAML by hand is tedious. Let’s look at the code window. There’s some sample code here for you already. It’s gonna put a text blocks on the screen, so here we’re defining a text box that’s going to say touch me and then we’re gonna add it to the canvas right here, set its font size in position. And then we’re going to define two different event handlers, light and dark. This one changes the foreground to white and this one changes it to black. And then we have a mouse enter and a mouse leave event and those are gonna call the methods, light and dark. So, we’re just gonna hit CTRL-Run to enter that, so you see what happens. I’ll go over here with my mouse and when I touch touch me, it becomes light or becomes dark again. So, there you have it, an example of how you can use a tool to experiment with dynamic Silverlight. I should warn you that it’s still a little rough and it might lock up on you. You’ll just have to restart CHIRON, but it’s pretty handy.

DLR Languages in Visual Studio

Visual Studio’s another tool you can use to create applications that use IronPython or IronRuby. There’s several ways that those languages can be used. They can be the language of the code behind page in a regular ASP.NET application or they can be used in the code behind of a Silverlight XAML file, or DLR code can be embedded within an application that’s written in a standard .NET language like C# or Visual Basic.

First let’s look at an example of an ASP.NET application written in IronPython. You see here that the page language at the top, at the first line there has been set to IronPython. Other than that, the page looks like a normal ASPX page. Here’s the code behind page and you see that it has a PY extension, disfaultaspx.py. Here’s where we define code to run at page load or when the button’s clicked. It’s the same thing we do in C# or Visual Basic, it’s just that we’re using IronPython instead.

We can also write DSL dynamic Silverlight applications using Visual Studio directly. Here’s how, there’s no template for a DSL application specifically. So, instead you first create a basic set up with SL.bat just like we did before. And in Visual Studio you open that site as a website. And that gives you access to the Ruby or Python files which you can edit however you need. Just like we did in the DLR console and in the CHIRON demo, you can change the XAML or the code or both, but you’ll have nicer Visual Studio tools to work with.

As you’re writing your code, you’re gonna wanna be able to run it for testing of course. Now, this can’t be done automatically in Visual Studio.You have to set up Visual Studio to understand that it needs to use CHIRON. So to do that you go to the property page for your project and set it up to use an external program CHIRON with a /b option for the browser and the website as the working directory. And after that, you can press F5 and when you’re project is usable.

And a third possibility is to write your application in a language like C# and then host an engine for Ruby or Python inside it to process code dynamically at runtime when you need to do that. Now, here’s what the code to do that looks like. First, we’ll create a class called Ruby engine and it’ll hold a private type script engine. In the constructor here, we’re gonna instantiate a new runtime and then use it to instantiate the engine. And then we create a method called execute that accepts a string argument. That string that comes into it’s gonna hold Ruby code that the user will enter dynamically at runtime and will pass that code to the engine for execution and will return the result. All of these classes that we’re using, by the way, script engine and script runtime and those are part of the IronRuby namespace that we’ll include.

The user interface has a text box called code where the user can enter arbitrary Ruby code at any time at runtime. And a text block called result that will display the result of executing that code. You see that graphically over here on the right. Now on the code behind page for the user interface page, will instantiate a Ruby engine. We declare it and then in our initialization here, we assign a new Ruby engine to that Ruby variable.

Then when the user presses a key on the key down event, we’ll check to see if it’s the inner key and if it is, we’ll send whatever code is in the code text box to the engine and that’ll return the evaluation that—the value that they have calculated, that the engine calculates, and that’ll be displayed the result text block. So you can see over here that the user types in 2+6 at runtime and it’s evaluated to be 8 and that’s what’s printed back out. So, this application’s going to allow us to write in C#, an application that can process arbitrary code at runtime using an embedded Ruby engine.

To use Visual Studio to create DSL applications, you’ll need to have access to Visual Studio of course, but it can be either the professional edition or it can be the web developer express edition and that’s a free download. Then you’ll also need to download Silverlight tools or Visual Studio version 2 which is the tools and templates that let you create a Silverlight project to develop Silverlight applications using C# or Visual Basic.

Visual Studio Silverlight Projects Demo

Now, let’s look at how to create some Visual Studio projects that use dynamic languages. We’ll start with something simple, creating a Silverlight application that uses Python. First, we’ll use the SL batch file to create our web app structure. I’m going to change to my SDK samples, script where I have my SL batch file. And I’ll use Python to create a test directory and I’ll just put this right at the root and I’ll call it test2.

Now I’m going to open Visual Studio and point it to our new skeleton app. So I’ll say file new and instead of project I’ll say website and I’ll point to the website that I put right there at the root. Now, this is a simple little website, it’s the same thing that we created, the same sort of skeleton that we created when we worked with SL batch before. It’s got an index and then inside Python it has our app Python and our app XAML. So we can run it right now, but to do that, we need to tell Visual Studio to use CHIRON to run it.

So, I’ll go to my start options and I’ll say start an external program. And that program will be CHIRON, I can browse for it right here. The command line argument is my /b for the browser. And the working directory is gonna be test2. Now, we can run it, we can execute it. Now, I can execute it without debugging because if I do start debugging, then I have 2 choices, either modify the configuration so the debugging is able, or run without debugging. Now, debugging is very handy, so normally I probably would enable it, but here I’m just gonna run it. And there’s my directory listing which we’ve seen before. If I open index site HTML, I’ll see my welcome to Python and Silverlight. And as we saw before, I could simply go in and begin to change my XAML or my Python and change the application to behaving whatever way I want it to behave.

Now let’s look at something a little fancier, the application that can host a scripting engine inside of a regular C# application. I’m going to open a starter application and run it. I’m going to run it so you can see that all it does is allow me to enter some code here, or anything here, really, and it will just simply echo it back. Now let’s look at the code that does this. Here in the page XAML, you can see that what we have is a text box and a text block. This one is called code, my text box is called code and my text block, which echoes the code back out is called result. Here in the app, the code behind, we can see if a key is pressed it checks to see if that key is enter, and if so, it simply takes what’s in code text and echoes it back out into result text and then clears the code text box.

Now let’s add some IronRuby to it. First I’ll add my references. If I go to the app itself, there’s a dependency’s folder that’s been added and it has references to the dlls that I need. I’m gonna add every one of ‘em. And now I need to write a quick rapper class to run my Ruby code. I’m gonna go here to my XAML which is written in C#, remember this is C# code that’s going to host our Ruby engine. I’m going to write a new class and this is the class we saw on the slides, it’s called Ruby engine. It declares a script engine type and then we’re going to create a runtime, this is just the standard way you do that, let me get this out of the way. And then using that runtime we’re going to get an engine and instantiate to that variable and then we have our execute here that will take in a string that is code and execute that code using the engine and return that.

Now, you can see that some of these aren’t recognized yet. That’s ‘cause we need a few little usings up here. So, I’ll just insert those now. I’m using my script hosting name space. I’m using IronRuby which has the Ruby engine things in it and I’m using the Silverlight scripting. Now, that’s all I need to run IronRuby code. I need to hook it up to the page now, so I’m going to go to my C# over here that’s the code behind for the page and there I’ll add an instance variable. I’m inside my class here and I’m going to declare that I have a Ruby engine. There it is, it’s popped up because it—we’ve added the reference and I’ll call it Ruby.

And then in my constructor here, I’ll make sure that Ruby gets assigned to and I’m gonna assign it in an instance of my class, my Ruby engine class that I just wrote. Then I need to change what happens in result text. Instead of just copying what’s in code text, what I’m going to do is I’m going to allow it—those two new lines in there, by the way to make sure that it moves down in my text block below my text box. So, two new lines to move it down to the right position and then a couple of carrots and then I’m gonna repeat what was typed into code text. And then after that, I’ll just put a new line so we can see what’s happening so it’ll be in a new location. And then instead of just doing what’s in result text, I’m going to send my code to the Ruby engine.

Okay, so I’ll send my code to the Ruby engine and print out what happens there. Now, I’m ready to build it and run it, got some warnings but I know what they’re about, don’t have to worry about them, it’s the errors I have to worry about. And I’m going to run it. Let’s type some Ruby code into the text box and see if it’ll work. It has to be a single string the way we’ve written it, we have a very simple thing, so I’ll just put in 2.2 and there we go, it moved down 2 lines, printed my 2 carrots, printed what was originally in the code text and then printed the result of running what I typed in there, the string I typed in there through the Ruby engine. This could be a Boolean by the way. It doesn’t have to be an expression that returns a number so let’s see if 3 is greater than 5. Apparently not, okay.

And because Ruby has a turnery operator, we could create a little one-line if structure if we wanted. If 3 is less than or equal to 5, then we’ll print yes and otherwise, we’ll print no, okay? 3 is less than or equal to 5. We can also ask if 3 is equal to 5. So you can see that we can do some simple logic things. We can have some little rules there. So, what we have now is a C# app, hosting the DLR and running arbitrary code that can include logical rules. A variation of this, this is very simple of course, but with a little work, we could execute those shopping rules that we talked about earlier. And the great thing is that we can change these things dynamically and have the application change in real time as we saw before, you don’t have to redeploy, you don’t have to restart, you don’t have to recompile. You just go and that’s one of the advantages of dynamic scripting.

And now that’s the end of this session on the fundamentals of the DLR. Here are the basic starting points for getting more information about the topics that we’ve covered. For the DLR and for IronPython you can start at Code Plex. IronRuby has its own site at www.ironruby.net and for Silverlight with dynamic languages, you can go to http://dynamicsilverlight.net Notice that you don’t have any www there. Now, these are just starting points, but they’ll provide you with links to lots of other material, including lots of demos and tutorials that are out there. The only thing that I want to warn you is that we’re still in development, we’re still in flux and some of the code you get is outdated or will not run with your tools and you have to be real careful about that, don’t get frustrated.

At the end of each session I like to remind you that the ISV Innovation site has lots of other great training videos. Today I wanna make sure that you know that there are additional training videos at ISV Innovation that go into the syntax of IronPython and IronRuby more deeply. So, one of those would be a logical next step if you’re interested in working with the DLR. Thanks for watching. Bye, bye.

[End of Audio]