Question: In C# its possibly to use [AsDynamic](http://blogs.msdn.com/b/davidebb/archive/2010/01/18/use-c-4-0-dynamic-to-drastically-simplify-your-private-reflection-code.aspx) to invoke a overloaded method on a subclass (example [here](https://github.com/gregoryyoung/m-r/blob/master/SimpleCQRS/Domain.cs)) thus allowing an abstract class to invoke methods it does not itself define. Is something similar possible with Scala 2.9 **applyDynamic** ?. I tried the following

```
abstract class AggregateRoot extends Dynamic {
  def applyChange(event: Event) {
    this.applyDynamic("handleEvent")(event) 
  }
  // this does not work
  def applyDynamic(name : String)(args: Any*) = this
}

```

used like this 

```
class InventoryItem extends AggregateRoot {
  def handleEvent(event: InventoryItemCreated) {
    println("received InventoryItemCreated")
  }

  def handleEvent(event: InventoryItemDeactivated) {
    println("received InventoryItemDeactivated")
  }
}

```

where **InventoryItemCreated** and **InventoryItemDeactivated** both are events

```
class Event;

class InventoryItemDeactivated extends Event; 

class InventoryItemCreated extends Event; 

```

I then expect to be able to do something like this

```
  var aggregate : AggregateRoot = new InventoryItem
  var event = new InventoryItemDeactivated
  aggregate.applyChange(event) // should print "received InventoryItemDeactivated"

```

but I cannot figure out how to define **applyDynamic** (in **AggregateRoot**) so that it can invoke overloaded methods in subclasses at runtime without itself defining them. Other solutions that achieve the same result are welcome (perhaps structural typing can come in handy ?).

Answer: 
You could try something like:

```
list<string> iterator it = whichList.begin();
std::advance(it, i);
if(*it == "") { /* ... */ }

```

But I think you need to clearly define what you mean by "empty" here - you can't compare strings to 0.

The key point is that `list` doesn't support random access - because of its implementation (a doubly-linked list), supporting random access would be an `O(n)` operation, i.e. linear in the length of the list in the worst case. That's inefficient, so it's deliberately not supported in the interface.

As others have pointed out, if you want random access then you're better off using something like a `vector` or `deque`. Generally speaking, you would use a `vector` if you only need fast insertion/removal at the end of the container, a `deque` if you also need fast insertion/removal at the front of the container, and a `list` only if you need fast insertion/removal in the middle of the container. In order to support the latter type of operation, `list` ends up sacrificing random access to elements.

See here for the definition of `advance`, incidentally:

<http://www.sgi.com/tech/stl/advance.html>

EDIT: As Alf pointed out, you can to some extent get fast insertion/removal in the middle of a vector using the gap buffer technique (see <http://en.wikipedia.org/wiki/Gap_buffer>), although an individual operation can be costly if you fill the gap (the idea is to amortise the cost over lots of operations, making an operation sequence comparatively cheap).
I think you should use a vector for this problem.