InfoQ

News

Duck Typing Using Runtime Code Generation

Posted by Jonathan Allen on Jul 08, 2008 12:55 AM

Community
.NET
Topics
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 Jul 10, 2008 12:36 PM
  1. Back to top

    do byte code enhancement instead of reflection

    Jul 10, 2008 12:36 PM 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

Bindings, Platforms, and Innovation

This presentation focuses on the Internet and separating myth from fact, history from the future, and the mundane from the imaginative. Bob Frankston presents a vision of what could and should be.

Orchestrating Long Running Activities with JBoss / JBPM

This article explores the use of JBoss and jBPM to implement design solutions that effectively address the issue of orchestrating long running activities.

Neo4j - The Benefits of Graph Databases

This presentation covers the use of graph databases as an optimal solution for data that is difficult to fit in static tables, rapidly evolving data or data that has a lot of optional attributes.

Realistic about Risk: Software development with Real Options

This session introduces Real Options and shows how it can help in running your project. Real Options is a decision-making process that can be used to manage risk.

Communication Flexibility Using Bindings

This article discusses the use of bindings on services and references (including the instance of non-configured bindings) as the means to implement SCA communications in a Web and SOA environment.

Writing DSLs in Groovy

After a short introduction to DSLs, Scott Davis plays with the keyboard showing how to approach the creation of a DSL by typing working snippets of Groovy code that get executed.

Scaling Agile with C/ALM (Collaborative Application Lifecycle Management)

IBM Rational and InfoQ present, Scaling Agile with C/ALM, an eBook showing organizations how to become “finely tuned software delivery machines” by enabling team integration and scaling.

Concurrent Programming with Microsoft F#

Amanda Laucher presents a real life enterprise application written in F#. She shows actual code snippets, explaining design decisions and suggesting how to use some of the F# constructs.