Pro DLR in .NET 4
Total Page:16
File Type:pdf, Size:1020Kb
Pro DLR in .NET 4 Chaur Wu Pro DLR in .NET 4 Copyright © 2010 by Chaur Wu All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN 978-1-4302-3066-3 ISBN 978-1-4302-3067-0 (eBook) Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. President and Publisher: Paul Manning Lead Editor: Jonathan Gennick Technical Reviewer: Scott Isaacs Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Jennifer L. Blackwell Copy Editor: Sharon Terdeman Compositor: Bytheway Publishing Services Indexer: Brenda Miller Artist: Integra Software Services Pvt. Ltd. Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. For information on translations, please e-mail [email protected], or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. I want to dedicate this book to Sarah, Everett, Cedric, and Chiachi CONTENTS AT A GLANCE Contents at a Glance About the Author.................................................................................................. xiv About the Technical Reviewer .............................................................................. xv Acknowledgments ............................................................................................... xvi Introduction ........................................................................................................ xvii PART 1 DLR Fundamentals ............................................................................1 Chapter 1: Introduction to DLR ...............................................................................3 Chapter 2: DLR Expression....................................................................................27 Chapter 3: Late Binding and Caching....................................................................65 Chapter 4: Late Binding and Interoperability........................................................87 Chapter 5: Dynamic Objects................................................................................109 Chapter 6: DLR Hosting API.................................................................................133 PART 2 Applying the DLR...........................................................................163 Chapter 7: DLR and Aspect-Oriented Programming ...........................................165 Chapter 8: Metaprogramming.............................................................................185 Chapter 9: Stitch — A DSL for Hosting Languages .............................................211 Chapter 10: Application Scripting.......................................................................239 Chapter 11: DLR in Silverlight.............................................................................251 Chapter 12: Dynamic Languages on JVM ...........................................................275 Index ...................................................................................................................297 iv CONTENTS Contents About the Author.................................................................................................. xiv About the Technical Reviewer .............................................................................. xv Acknowledgments ............................................................................................... xvi Introduction ........................................................................................................ xvii PART 1 DLR Fundamentals ............................................................................1 Chapter 1: Introduction to DLR ...............................................................................3 Setting Up Code Examples.................................................................................................4 Software Requirements............................................................................................................................ 4 Installing the DLR, IronPython, and IronRuby ........................................................................................... 5 Hello World Examples........................................................................................................6 Implementing REPL for the Hello Language ............................................................................................. 9 Practical Uses for the DLR .................................................................................................9 Application Scripting and DSL ................................................................................................................ 10 XML Builder ............................................................................................................................................ 10 Aspect-Oriented Programming............................................................................................................... 11 Runtime ...........................................................................................................................13 Runtime vs. Run Time............................................................................................................................. 14 Run Time vs. Compile Time.................................................................................................................... 14 Dynamic vs. Static...........................................................................................................16 Dynamic Typing...................................................................................................................................... 17 Dynamic Dispatch................................................................................................................................... 20 Language.........................................................................................................................21 Programming Languages in Practice...............................................................................22 v CONTENTS Putting It Together...........................................................................................................24 Summary .........................................................................................................................25 Chapter 2: DLR Expression....................................................................................27 DLR Expression as a Language........................................................................................27 Code as Data .......................................................................................................................................... 28 A Common Denominator like CLR........................................................................................................... 28 Concrete Syntax and Serialization.......................................................................................................... 29 Expressions vs. Statements.............................................................................................29 Expression Type and Kind................................................................................................31 Binary Expressions ..........................................................................................................33 Flow Control Expressions ................................................................................................36 If-Then-Else Expressions........................................................................................................................ 37 Switch Expressions ................................................................................................................................ 38 Scopes and Name