The Windows Runtime IDL compiler lets you abbreviate some interface names

This post has been republished via RSS; it originally appeared at: Microsoft Developer Blogs - Feed.

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.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.