Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is DefaultOverloadAttribute required in WinRT?

I recently had a WinRT class definition with methods like this:

public void Foo(string x){}
public void Foo(CustomClass x){}

The compiler threw an error though that

The 1-parameter overloads of Earlz.FooBar must have exactly one method specified as the default overload by decorating it with Windows.Foundation.Metadata.DefaultOverloadAttribute.

What kind of implications does this attribtue have? I'm unfamiliar with how WinRT projections work, but what I'm writing will eventually be a publicly usable API. So, I want to make sure that I'm not doing something that will cause pain to people who use the API. Should I rename my Foo method to FooCustom or some such, or is using DefaultOverload on the more commonly used function the way to go?

Also, I tried looking up what this attribute does and why it's required, but MSDN was short on details as usual and just gave single sentence description "Indicates that a method is the default overload method"

I assume the primary reason for this attribute is because Javascript only supports overloading on parameter count, not parameter type. However, how would a Javascript app access the non-default WinRT methods with type overloads?

like image 951
Earlz Avatar asked Jan 22 '13 16:01

Earlz


1 Answers

As Jesse Jiang described in this thread:

JavaScript has limited ability to differentiate overloaded methods. Therefore, you need to change the function name, or setting the Windows::Foundation::Metadata::DefaultOverloadAttribute to set the default overload function.

like image 71
Jared Bienz - MSFT Avatar answered Nov 20 '22 12:11

Jared Bienz - MSFT