microsoft / xlang

MIT License
875 stars 103 forks source link

Support methods with default parameters #764

Closed JaiganeshKumaran closed 1 year ago

JaiganeshKumaran commented 2 years ago

Add support for methods with default parameters to author richer APIs. Today it's already possible in C++/WinRT to expose methods with default parameters by emitting overloads for the method in metadata however you can't do the same when writing Windows Runtime APIs in other languages as the separation between projection and implemented is much tighter. However, when you do this, the calling side doesn't it's really a default parameter and not an overload and they can't know what the parameter's default value is without reading the API documentation (if provided). Instead use an attribute approach to specify a default parameter in the metadata so language projections could emit the method with the last few parameters being the default. Under the hood, instead of an overload, it will just be the original method and the default parameter won't be there (I'm not sure if the ABI allows for default parameters) however projections will look like it has a default parameter. The benefit is that the semantics from the calling side will be visible.

Examples

Declaration in IDL: [default(defaultFlag, true)] void SomeMethod(SomeType value, Boolean defaultFlag);

Implementation in C++: void SomeMethod(SomeType value, bool defaultFlag /* = true */) Since the default value has been already specified in the method, only needed when you want the same behaviour when accessing the implementation side. The default parameter attribute has no effect in the ABI or implementation layer.

Accessing projection from C++: SomeMethod(SomeValue, /* true */);

Declaration + implementation in C#

// Default parameter attributed added to method.
void SomeMethod(SomeType value, bool defaultValue = true);

Accessing projection from C# SomeMethod(SomeValue, true);

github-actions[bot] commented 1 year ago

This issue is stale because it has been open 10 days with no activity. Remove stale label or comment or this will be closed in 5 days.