[Proposal]Allow implicit calling operator overloading in interface #3397
Replies: 7 comments
-
@huoshan12345 This seems wrong but my opinions aside, DIM was consciously designed such that it isn't part of the contract of the type so it wouldn't break source or binary compatibility so in this case you might want to use an |
Beta Was this translation helpful? Give feedback.
-
@eyalsk I would like to apply it for a
OR
However it seems to be much more difficult to implement any of them. |
Beta Was this translation helpful? Give feedback.
-
@huoshan12345 Why can't you do something like this? void Main()
{
var a = new ServiceA();
var b = new ServiceB();
var c = a.Add(b);
}
interface IService
{
IService Execute();
}
class ServiceA : IService
{
public IService Execute()
{
Console.WriteLine(typeof(ServiceA));
return this;
}
}
struct ServiceB : IService
{
public IService Execute()
{
Console.WriteLine(typeof(ServiceB));
return this;
}
}
struct ServiceComposer : IService
{
private IService _a;
private IService _b;
public ServiceComposer(IService a, IService b)
{
_a = a;
_b = b;
}
public IService Execute()
{
Console.WriteLine(typeof(ServiceComposer));
_a.Execute();
_b.Execute();
return this;
}
}
static class IServiceExtensions
{
public static IService Add(this IService a, IService b) => new ServiceComposer(a, b);
} Maybe even change
I'm not sure what you want to do with this but yeah, I think that this is much better than a language change. |
Beta Was this translation helpful? Give feedback.
-
@eyalsk extension method is great and it can totally serve my purpose |
Beta Was this translation helpful? Give feedback.
-
@huoshan12345 I understand what you're saying but given the community discussions we had about DIM and the design goals for it I doubt the LDT would change their minds about it but like always I could be wrong. 😄
You're violating the design guidelines for operator overloading but more importantly I really don't think it's more readable because I don't really know what adding a service means whereas I do know what adding one number to another would mean. |
Beta Was this translation helpful? Give feedback.
-
I agree with you. 😅
Service may be a bad name as an example.
And I would like to call like this:
|
Beta Was this translation helpful? Give feedback.
-
@huoshan12345 Yes this makes more sense; however, in C# 10 we gonna have something called Shapes/Roles or something similar (hopefully!) #164/#1711 which is tracked here #110 that would allow you to do things over types in a more general way so you could do exactly what you want and more, for example with shapes, you could define a shape say |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Now an operator overloading can be defined in interface like this:
And can be called like this:
OR
OR
However, the operator overloading in interface has to be called by interface explicitly which means it cannot be called like this:
It would be great if I could do the above.
Beta Was this translation helpful? Give feedback.
All reactions