The Windows Runtime IDL compiler lets you abbreviate some interface names

Raymond Chen

Raymond

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:

ShorthandExpands 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.

 

3 comments

Comments are closed. Login to edit/delete your existing comments

  • Avatar
    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.

  • Gunnar Dalsnes
    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
      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 be used ahead of those from the library. Mainly applied when not using OO/class style programming