March 16th, 2020

The Windows Runtime IDL compiler lets you abbreviate some interface names

If you use an unqualified name in a Windows Runtime IDL file, it is looked up in the current namespace. If you need a name from another namespace, you need to provide its full name.

namespace Contoso.Widgets
{
  runtimeclass Widget
  {
    Windows.Foundation.Collections.IVectorView<String> GetNames();
  }
}

There is an exception to this rule: If a parameterized type is given without a namespace, then the Windows Runtime IDL compiler will look in the Windows.Foundation.Collections namespace before giving up. In practice, this means that you can use the following shorthand:

Shorthand Expands to
IIterable<T> Windows.Foundation.Collections.IIterable<T>
IIterator<T> Windows.Foundation.Collections.IIterator<T>
IKeyValuePair<K, V> Windows.Foundation.Collections.IKeyValuePair<K, V>
IMap<K, V> Windows.Foundation.Collections.IMap<K, V>
IMapChangedEventArgs<K> Windows.Foundation.Collections.IMapChangedEventArgs<K>
IMapView<K, V> Windows.Foundation.Collections.IMapView<K, V>
IObservableMap<K, V> Windows.Foundation.Collections.IObservableMap<K, V>
IObservableVector<T> Windows.Foundation.Collections.IObservableVector<T>
IVector<T> Windows.Foundation.Collections.IVector<T>
IVectorView<T> Windows.Foundation.Collections.IVectorView<T>
MapChangedEventHandler<K, V> Windows.Foundation.Collections.MapChangedEventHandler<K, V>
VectorChangedEventHandler<T> Windows.Foundation.Collections.VectorChangedEventHandler<T>

Resulting in

namespace Contoso.Widgets
{
  runtimeclass Widget
  {
    IVectorView<String> GetNames();
  }
}

Unfortunately, this courtesy does not apply to the Windows.Foundation namespace. You still have to write the full name Windows.Foundation.IAsyncAction, for example.

Bonus chatter: Why does the shorthand work for Windows.Foundation.Collections but not Windows.Foundation? Simple: When the compiler was being written, nobody asked for a shorthand for the Windows.Foundation namespace.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

3 comments

Discussion is closed. Login to edit/delete existing comments.

  • Gunnar Dalsnes

    “nobody asked for a shorthand ” it is not really a shorthand IMO, it is a failover. and having multiple failovers…then you get into fights over failover priority… so thank godness it stopped with this single failover.

    • Ian Yates

      Agreed. Name resolution is hard.

      Back in the Delphi / pascal days, just changing the order of files in your using clause (used in interface & implementation sections of the file) would change the order of imports of functions, and thus possibly affect your code in subtle ways.

      It was used (abused?) to allow overrides of functions by putting your own super version of some library routines first in the using clause so its versions would...

      Read more
  • Alex Martin

    This seems a lot like one of those little features you add that seems like a good idea at the time but then it catches people by surprise and causes confusion later.