Allow operators to be defined as instance methods. #8787
Replies: 62 comments
-
Can you add this scenario to your proposal please? |
Beta Was this translation helpful? Give feedback.
-
@YairHalberstadt , sure, no problem. I just assumed, that everyone tried this hundreds of times, and know what I am talking about. UPD. Moved the code sample into the initial post. |
Beta Was this translation helpful? Give feedback.
-
Never assume 🙂 |
Beta Was this translation helpful? Give feedback.
-
There are various proposals around type classes that aim to address this problem from the opposite angle. Namely, allow interfaces to declare static members. One disadvantage is they will only work in generic contexts. |
Beta Was this translation helpful? Give feedback.
-
@YairHalberstadt type classes require CLR changes, or type erasure. This needs neither. |
Beta Was this translation helpful? Give feedback.
-
@lostmsu |
Beta Was this translation helpful? Give feedback.
-
@YairHalberstadt the shapes proposal seems to cover what I am looking for, but the implementation cost would be much higher, and would introduce generic parameter bloat. Since CLR has (or at least used to have) very tight restrictions on the number of generic parameters, it might not be viable. |
Beta Was this translation helpful? Give feedback.
-
The shapes proposal was proposed by Mads Torgerson, the program manager for C#. They are likely going to look very very hard into implementing it. It could be it isn't actually manageable, but I would hold on for now... |
Beta Was this translation helpful? Give feedback.
-
@YairHalberstadt well, I've done that for F# back in university ~10 years ago: https://archive.codeplex.com/?p=ntypeclasses |
Beta Was this translation helpful? Give feedback.
-
Elegant, as in elegant to use, or elegant as in elegant CodeGen? |
Beta Was this translation helpful? Give feedback.
-
Shapes/TypeClasses/etc. has a lot of interest at the LDM level. There are many reasons for this. However, one of hte primary ones is that it's felt that there are so many issues/proposals/things-people-want that wuold just fall under such an umbrella. So, instead of having to define 50 different features to do everythin in a one-off fashion, you just define the one large and flexible feature that subsumes all the rest at an acceptable-enough level. |
Beta Was this translation helpful? Give feedback.
-
@YairHalberstadt elegant as in design and semantics, and also codegen. |
Beta Was this translation helpful? Give feedback.
-
@CyrusNajmabadi , as far as I can see, having type classes would not make the code in the example as elegant as it could be, because you'd still have to call |
Beta Was this translation helpful? Give feedback.
-
You should be able to define operators in type classes, and by constraining a type parameter to a type class you would be able to write |
Beta Was this translation helpful? Give feedback.
-
@YairHalberstadt , as under the hood the type classes are represented as interfaces, that would imply you can define operators on interfaces. |
Beta Was this translation helpful? Give feedback.
-
@HaloFour what is the basis then? |
Beta Was this translation helpful? Give feedback.
-
Whether the feature solves problems and fits within long-term vision of the language. |
Beta Was this translation helpful? Give feedback.
-
As are "jokes". You're the one who opened up that part of the discussion. I'm just pointing out that that it's not an the objective of hte C# language to minimize people making jokes about it. |
Beta Was this translation helpful? Give feedback.
-
And I am explaining why it should. However, I did not see any argument against time-boxing the shapes feature, except that it is the way things are. |
Beta Was this translation helpful? Give feedback.
-
Your explanations run contrary to the LDM's goals and hte way they look at and assess features (especially features which are considered to be subsumed by some umbrella feature).
Correct. It i the way that things are. The LDM has chosen that. |
Beta Was this translation helpful? Give feedback.
-
Because that does not lead to good design. Time box a spike, sure, as to not spin out down a rabbit hole. But that doesn't apply to the design of the entire feature, or whether or not it will ship, regardless of when it might ship. C# will never adopt an approach of trying to force features into a release. If that's what you need from a language you'd be better served elsewhere. |
Beta Was this translation helpful? Give feedback.
-
indeed, it's often the opposite. If the feature isn't good enough, it will often get pulled in order to not run the risk of:
|
Beta Was this translation helpful? Give feedback.
-
What are LDM's goals? I want to be clear on this. |
Beta Was this translation helpful? Give feedback.
-
To ship the features they feel are appropriate on the timeline they deem best. |
Beta Was this translation helpful? Give feedback.
-
That sounds quite terrible. |
Beta Was this translation helpful? Give feedback.
-
@lostmsu This doesn't seem to be going anywhere. If you would like to dsicuss things more, i highly recommend gitter.im/dotnet/csharplang or gitter.im/dotnet/roslyn Many community and team members participate there (including former and current LDM members) and it's far more conducive to this sort of discussion. |
Beta Was this translation helpful? Give feedback.
-
Oh well. |
Beta Was this translation helpful? Give feedback.
-
Gitter does not have threads, neither does GitHub unfortunately. |
Beta Was this translation helpful? Give feedback.
-
Oh well. It's up to you. It sounds like you would like to have a discussion of the process around language changes. gitter.im/dotnet/csharplang is likely to be a far better venue for that versus this github issue. |
Beta Was this translation helpful? Give feedback.
-
Closed a developer community feedback as duplicate. |
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.
-
Yes, we've read https://blogs.msdn.microsoft.com/ericlippert/2007/05/14/why-are-overloaded-operators-always-static-in-c/
However, there are many scenarios, when having operators to be static methods fail. Main one being inability to represent generic algebras, which is hindering efforts to make C# suitable for scientific computing.
My proposal is simple: allow defining operators without
static
keyword, and permit them on interfaces. When performing operator overloading, always choosestatic
operators defined in 7.0 way, if present, and issue a warning, if there is also a matching instance operator.E.g.
P.S. IMHO, the fact, that users of the feature can shoot themselves in the feet by making asymmetric addition that way should not prevent legitimate use. There are users, who would be able to do that with inheritance, but nobody is arguing for it to be deprecated.
Beta Was this translation helpful? Give feedback.
All reactions