InfoQ

InfoQ

News

My Bookmarks

Login or Register to enable bookmarks for unlimited time.

The content has been bookmarked!

There was an error bookmarking this content! Please retry.

Duck Typing Using Runtime Code Generation

Posted by Jonathan Allen on Jul 08, 2008

Sections
Development,
Architecture & Design
Topics
.NET ,
Programming
Tags
C# ,
Duck Typing

Duck typing techniques can be used in statically typed languages like C#, but it generally requires some tedious reflection code. But seeing the benefit of such techniques, some are turning to it anyways and are developing ways to make it less painful.

The concept behind duck typing, also known as late binding, is pretty simple; if an object responds to all the methods a function is expected to call, that object can be passed to the function. Those more familiar with static typing may say, "Well isn't that just an Interface?" Well yes and no, while conceptually it is like an Interface, it does not have to be statically defined. This is a big deal when the class is not created by the developer using it.

One option always available is to create an adapter class. The adapter implements the interface and passes the method calls unto the real object. Unfortunately, building these adapter classes is tedious. In order to make this less tedious and more flexible, developers are starting to turn to runtime code generation.

Examples of this can be seen in several open source projects including:

These may end up being stop-gap measure. Visual Basic already has duck typing via Option Strict Off. Dynamically implied interfaces were also considered for VB 9, and though it didn’t make the cut we may see it in VB 10. Meanwhile C# is considering adding a dynamic keyword to gain the same effect as Option Strict Off.

do byte code enhancement instead of reflection by Roger Voss Posted
  1. Back to top

    do byte code enhancement instead of reflection

    by Roger Voss

    reflection is slow

    do byte code enhancement, ala the zillion of tools, libraries, frameworks, and compilers that have done this for years with Java

    for instance, with AspectJ, one can do compile-time enhancement of classes where weaving of aspects happens then

    or weaving can be done at class load time

    in either case, the byte code is edited and the resulting enhanced byte code is used for actual execution

    and of course practically anything can be done and generated by such technique - including generating duck-type interfaces at runtime

Educational Content

Jesper Boeg on Priming Kanban

In this interview, Jesper Boeg, author of the new InfoQ book – Priming Kanban, discusses the keys to using Kanban effectively, and how to get started if you are currently using other approaches.

New-age Transactional Systems - Not Your Grandpa's OLTP

John Hugg discusses high volume transaction processing applications with high and low frequency profiles, and how VoltDB can be used for that purpose.

Cool Code

Kevlin Henney examines code samples to see what can be learned from them starting from the premise that one won’t write great code unless he knows how to read it.

Collaboration: At the Extremities of Extreme

Jason Ayers share the observations he made watching a team of developers collaborating in real time on the same code base, pushing XP, pair programming and continuous integration to their extremes.

Yesod Web Framework

Michael Snoyman presents Yesod, a web framework written in Haskell and containing a web server, templating, ORM, libraries (templating, gravatar, etc.).

Transactions without Transactions

Richard Kreuter and Kyle Banker on how to avoid classical RDBMS transactional systems by using compensation mechanisms, transactional messaging or transactional procedures.

Attila Szegedi on JVM and GC Performance Tuning at Twitter

Attila Szegedi talks about performance tuning Java and Scala programs at Twitter: how to approach GC problems, the importance of asynchronous I/O, when to use MySQL/Cassandra/Redis, and much more.

10 tips on how to prevent business value risk

One category of risk that project teams need to ensure they address is business value failure – delivering a product that fails to provide value for the business investor.