InfoQ

News

C# Feature Focus: Dynamically Typed Objects, Duck Typing, and Multiple Dispatch

Posted by Jonathan Allen on Oct 27, 2008

Community
.NET
Topics
Dynamic Languages ,
Language Design
Tags
C#

Before we dig into the first C# feature, let it be known that Microsoft has promised that any capability found in C# will be offered in VB and vice versa in some fashion. However, they will not necessarily offer it the same way and the languages are expected to continue to diverge.

With the increasing importance of dynamic languages and the DLR, C# needs to be able to work with dynamically typed objects. Currently it is possible to make late bound calls via reflection for static classes, but this takes a lot of code. Moreover, calls to DLR objects require an entirely different call using the DLR's reflection libraries.

In C#, you simply declare the object's static type as "dynamic". Like VB's Option Explicit Off, this tells the compiler to emit the necessary code to resolve the method binding at runtime calls. At the IL, variables declared as dynamic are really of type System.Object with an additional tag indicating it uses dynamic calling semantics.

At runtime all of the normal overload resolution rules work against the object's runtime type. This means you get the ability to perform multiple dispatch directly instead of jumping through reflection or using visitor patterns.

Each dynamic language has its own rules for member lookup. To support this object can implement the IDynamicObject interface. If it exists on the runtime object, the object gets to handle its own member lookup. In the demonstration, Anders showed how to define a dynamic object in C# itself.

And of course, all this means you can use duck typing wherever you want in C#.

Code Example by Chuck Canning Posted Oct 27, 2008 8:04 PM
Re: Code Example by Jonathan Allen Posted Oct 28, 2008 1:10 AM
Re: Code Example by Pablo García Posted Oct 28, 2008 6:33 AM
Re: Code Example by Jonathan Allen Posted Oct 28, 2008 9:52 AM
Multiple dispatch by srdjan marinovic Posted Oct 28, 2008 8:36 AM
Re: Multiple dispatch by Jonathan Allen Posted Oct 28, 2008 9:57 AM
  1. Back to top

    Code Example

    Oct 27, 2008 8:04 PM by Chuck Canning

    It would be nice to show an example of what this would look like to the developer.

  2. Back to top

    Re: Code Example

    Oct 28, 2008 1:10 AM by Jonathan Allen

    Sorry about that. Here is an example:


    dynamic obj = [some function];
    obj.Foo();

    As you can see, there really isn't much to it.

  3. Back to top

    Re: Code Example

    Oct 28, 2008 6:33 AM by Pablo García

    It would be nicer to do:
    var obj = [some function];
    obj.Foo();

  4. Back to top

    Multiple dispatch

    Oct 28, 2008 8:36 AM by srdjan marinovic

    I am quite curious about this quote: "At runtime all of the normal overload resolution rules work against the object's runtime type. This means you get the ability to perform multiple dispatch directly..."

    Does this mean that the dispatching is done on all runtime types for the method parameters, ala CLOS? And how is this specific to dynamic type?

    Thank you for the time and help

    Kind regards

    Srdjan

  5. Back to top

    Re: Code Example

    Oct 28, 2008 9:52 AM by Jonathan Allen

    The thing is, var is just a place holder while dynamic is a type. This means you can write:


    string GetText(dynamic foo){
    return foo.Text;
    }

  6. Back to top

    Re: Multiple dispatch

    Oct 28, 2008 9:57 AM by Jonathan Allen

    Lets say you have this code:


    void Foo(dynamic a, dynamic b, dynamic c) {
    a.Bar(b, c)
    }


    What happens is the runtime asks object a "What should happen when I call 'Bar' with types b.GetType and c.GetType?". If a is a python object the answer may very well be different than is a is a C# object.

    Since the decision is being made entirely at runtime, I would expect you see to see CLOS-like dispatching.

Educational Content

Brian Marick on 4 Challenges and 5 Guiding Values of Agile Software Development

Brian Marick takes us through a quick tour of the most important values and challenges to adopting Agile successfully (they aren't the typical challenges and values we hear in the community).

Are You a Software Architect?

The line between development and architecture is tricky. Does it exist at all? Is an ivory tower actually needed? There's a balance in the middle, but how do you move from developer to architect?

Agile – A Way of Life and Pragmatic Use of Authority

The word 'authority' sometimes produces an allergic response in hard-line agilists. Freedom and authority – both are bad if misused and both are good if used in right spirit for a noble cause.

Getting Started with Grails, Second Edition

"Getting Started with Grails" brings you up to speed on this modern web framework. Companies as varied as LinkedIn, Wired, and Taco Bell are all using Grails. Are you ready to get started as well?

Using ITIL V3 as a Foundation for SOA Governance

Those familiar with only ITIL V2 often scoff at the thought that ITIL could serve as a governance framework for SOA. With ITIL V3, the focus of the framework shifted towards service-orientation.

Adrian Colyer on AspectJ, tc Server and dm Server

SpringSource CTO Adrian Colyer discusses AspectJ, SpringSource's dm Server and tc Server products, OSGi and Scrum.

Adam Wiggins on Heroku

Heroku's Adam Wiggins talks about Rails, Background Jobs, Add-Ons, Ruby, and how Heroku manages to work around Ruby's inefficiencies using Erlang and other languages.

SOA as an Architectural Pattern: Best Practices in Software Architecture

For Grady Booch the foundation of a good architecture is patterns, SOA being just one of many patterns. In this Second Life presentation, Booch attempts to bring more clarity on what architecture is.