Lập trình .net 4.0 và visual studio 2010 part 9 pps

6 354 0
Lập trình .net 4.0 và visual studio 2010 part 9 pps

Đang tải... (xem toàn văn)

Thông tin tài liệu

CHAPTER 3  LANGUAGE AND DYNAMIC CHANGES 61 Figure 3-3. ResolverOne application One of the developers on ResolverOne was Michael Foord, who is author of IronPython in Action (Manning Publications, 2009). I spoke to Michael about his experiences with working with embedding dynamic languages and IronPython. Michael Foord Why should VB.NET/C# developers be interested in IronPython? Much of the discussion here applies to other dynamic languages, including IronRuby, but Python is my particular area of expertise. IronPython is a .NET implementation of the popular open source programming language Python. Python is an expressive language that is easy to learn and supports several different programming styles, including interactive, scripting, procedural, functional, object-oriented, and metaprogramming. But what can you do with IronPython that isn’t already easy with your existing tools? The first entry in the list of programming styles is “interactive.” The IronPython distribution includes ipy.exe, the executable for running scripts or programs that also doubles as an interactive interpreter. When you run ipy.exe, you can enter Python code that is evaluated immediately and the result returned. It is a powerful tool for exploring assemblies and learning how to use new frameworks and classes by working with live objects. The second reason to use IronPython is also the second programming style in the list: scripting. Python makes an excellent tool for generating XML from templates, automating build tasks, and a host of other everyday operations. Because scripts can be executed without compilation, experimentation is simple and fast. Python often creeps into businesses as a scripting language, but beware it spreads. CHAPTER 3  LANGUAGE AND DYNAMIC CHANGES 62 One of the big use cases for IronPython is for embedding in applications. Potential uses include user scripting, adding a live console for debugging, creating domain-specific languages (DSLs) where rules can be added or modified at runtime, or even building hybrid applications using several languages. Python has several features, such as the ability to customize attribute access, that make it particularly suited to the creation of lightweight DSLs. IronPython has been designed with these uses in mind and has a straightforward hosting API. There are many areas where dynamic languages are fundamentally different from statically typed languages, a topic that rouses strong opinions. Here are a few features of IronPython that make it easy to develop with: • No type declarations • First class and higher order functions • No need for generics; it uses flexible container types instead • Protocols and duck-typing instead of compiler enforced interfaces • First class types and namespaces that can be modified at runtime • Easier to test than statically typed languages • Easy introspection (reflection without the pain) • Problems like covariance, contravariance and casting just disappear The best way to learn how to get the best from IronPython is my book IronPython in Action. I've also written a series of articles aimed at .NET developers to help get you started, including • Introduction to IronPython (http://www.voidspace.org.uk/ironpython/ introduction-to-ironpython.shtml) • Python for .NET Programmers (http://www.voidspace.org.uk/ironpython/python- for-programmers.shtml) • Tools and IDEs for IronPython (http://www.voidspace.org.uk/ironpython/tools- and-ides.shtml) Happy experimenting. What does Resolver One’s Python interface provide that VBA couldn’t? The calculation model for Resolver One is very different from Excel. The data and formulae you enter in the grid is translated into an interpreted language and you put your own code into the flow of the spreadsheet, working on the exact same object model that your formulae do. Having the programming model at the heart of Resolver One was always the core idea. When development started, the two developers (a few months before I joined Resolver Systems) evaluated interpreted languages available for .NET. When they tried IronPython they made three important discoveries: • Although neither of them was familiar with Python, it was an elegant and expressive language that was easy to learn. • The .NET integration of IronPython was superb. In fact, it seemed that everything they needed to develop Resolver One was accessible from IronPython. • As a dynamic language, Python was orders of magnitude easier to test than languages they had worked with previously. This particularly suited the test-driven approach they were using. CHAPTER 3  LANGUAGE AND DYNAMIC CHANGES 63 So the main advantage of Resolver One is that programmability is right at the heart of the spreadsheet model. IronPython is generally regarded as being a much “nicer” language than VBA. Python is a dynamically typed, cross-platform, open source, object-oriented, high-level programming language. Python was first released publicly in 1991, making it older than C#, and is widely used in many different fields. What do you think of the new dynamic features in .NET? They’re great, particularly for interoperating between C# and DLR-based languages. The dynamic features make this much easier. The dynamic keyword also makes creating fluent APIs possible (like the way you access the DOM using the document object in Javascript). This is particularly useful for DSLs. Duck typing is one of the features of dynamic languages that simplify architecture. I doubt that the dynamic keyword, will be used much for this however, as it doesn’t gel well with the way most .NET developers use traditional .NET languages. Apart from your book (obviously), any recommended reading on Python or dynamic languages? The Python tutorial and documentation is pretty good. Unsurprisingly they can be found from the Python website at http://www.python.org/. There is an interactive online version of the Python tutorial created with IronPython and Silverlight at: http://www.trypython.org/. For learning IronPython there is an excellent community resource called the IronPython Cookbook: http://www.ironpython.info/. For more general Python resources I recommend Dive into Python and the Python Essential Reference. F# F# is a functional programming language for the .NET framework that was previously available as a separate download to Visual Studio but now comes included in VS2010. Some developers feel that functional languages such as F# can enable you to work in a more intuitive way (particularly for those with a mathematical background), and are very good at manipulating sets of data and for solving mathematical and scientific problems. Interest in functional languages is increasing due to their absence of side effects (where an application modifies state as well as returning a value). The lack of side effects is vital in multithreaded and parallelized applications (see Chapter 5). Note that F# is not as strict as some functional languages and allows the creation of non-functional constructs, such as local variables. So should you rush out and learn F#? Well it’s not going to take over C# or VB.NET for developing line of business applications, but it is worth noting that functional languages have been influencing the development of C# and VB. An example is the recent addition of traditionally functional features, such as lambda expressions. However, I do believe that looking at other languages can help make you a better programmer (I’m currently looking into Python). At a DevEvening user group presentation, Jon Skeet suggested to us that functional languages may help you become a better developer by getting you to think about a problem in a different way. For a great introduction to F# view the presentation at: http://mschnlnine.vo.llnwd.net/d1/ pdc08/WMV-HQ/TL11.wmv. And then take a look at the official site at: http://research.microsoft.com/en-us/um/cambridge/ projects/fsharp/default.aspx CHAPTER 3  LANGUAGE AND DYNAMIC CHANGES 64 Jon Skeet For those developers who really want to delve into the details of a language, I don’t think you can do much better than read Jon Skeet’s C# In Depth (Manning Publications, 2008). A revised version for .NET 4.0 is currently on its way (http://csharpindepth.com/). I spoke to Jon about his thoughts on C# 2010. What Do You See as the Top Feature(s) in C#2010, and Why? Named arguments and optional parameters, without a doubt. (That sounds like two features, but they naturally come together.) It's a small feature, but it’s likely to be the most widely used one. Two of the others (better COM support and dynamic typing) are only likely to be used by a minority of developers, and while generic variance is useful and interesting, it’s more of a matter of removing a previous inconvenience than really introducing something new. Admittedly, to fully take advantage of optional parameters, you have to be confident that all your callers will be using a language supporting them. For example, suppose you wanted to write a method with five parameters, three of them optional. Previously you may have used several overloads to avoid forcing callers to specify arguments for parameters where they’re happy with the default. Now, if you don't care about (say) C#2008 callers, you can just provide a single method. But that would force any C#2008 callers to specify all the arguments explicitly. The biggest potential use I see for the feature is immutability. C#2008 made it easy to create instances of mutable types using object initializers, but provided no extra support for immutable types. Now with named arguments and optional parameters, it’s a lot easier. For example, take the initialization of a mutable type: Person p = new Person { Name = "Jon", Occupation = "Software Engineer", Location = "UK" }; This can be converted into initialization of an immutable type without losing the benefits of optional data and explicitly specifying which value means what: Person p = new Person ( name: "Jon", occupation: "Software Engineer", location: "UK" ); Are There any New Features in C#2010 That You Would Avoid Using or That Have the Potential to Encourage Bad Programming? Well, dynamic typing is going to be really useful when you need it, but should generally be avoided otherwise, in my view. It’s great for interoperability with dynamic languages via the DLR, some additional COM support, and occasional cases where you would otherwise use Reflection. But C# is basically a statically typed language. It isn’t designed for dynamic typing. If you want to use dynamic typing widely throughout a program, write it in a dynamic language to start with. You can always use the DLR to work with C# code as well, of course. CHAPTER 3  LANGUAGE AND DYNAMIC CHANGES 65 What Would You Like to See in the Next Version of C#? More support for immutability. For example, readonly automatic properties would be a simple but really helpful feature: public string Name { get; readonly set; } That would be backed by a readonly field behind the scenes, and the property could only be set in the constructor (where the assignment would be converted into simple field assignment). Beyond that, the ability to declare that a type is meant to be immutable, with additional compiler support and checking, would be great. But that's a bigger request. If Code Contracts takes off, it would also be nice to embed the simplest contract, non-nullity, into the language, in the way that Spec# did. For example, instead of: public string Convert(string input) { Contract.Requires(input != null); Contract.Ensures(Contract.Result<string>() != null); // Do processing here } you could just write: public string! Convert(string! input) { // Do processing here } The handling of non-nullable local variables could be tricky, but there are smart people at Microsoft. I'm sure they could figure something out. Admittedly I’m wary of anything that links the language too closely to specific libraries, but this would be a really nice win in terms of readability. These may all sound like I’m lacking in ambition. After all, there are bigger ideas on the table such as metaprogramming. But I’m really keen on small, simple changes that make a big difference. I generally need to be persuaded more when it comes to large changes. The ones we’ve already had in C# have been very well designed, and I’m pleased with them. But the language is getting really pretty big now, and I think we need to make sure it doesn't become simply too hard to learn from scratch. Future of C# At the 2008 PDC Anders Hejlsberg noted that many developers were creating programs that created programs (meta programming). Anders considered it would be useful to expose compiler methods to developers to give them complete control over compilation. In a future version of .NET the compiler will be written in managed code and certain functions made accessible to the developer. Anders then demonstrated an example of this by showing a REPL (Read Evaluate Print Loop) C# application. . revised version for .NET 4. 0 is currently on its way (http://csharpindepth.com/). I spoke to Jon about his thoughts on C# 201 0. What Do You See as the Top Feature(s) in C# 201 0, and Why? Named. functional programming language for the .NET framework that was previously available as a separate download to Visual Studio but now comes included in VS 201 0. Some developers feel that functional. (Manning Publications, 200 9) . I spoke to Michael about his experiences with working with embedding dynamic languages and IronPython. Michael Foord Why should VB .NET/ C# developers be interested

Ngày đăng: 01/07/2014, 21:20

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan