Make the interface support implicit operators? #7598
Replies: 4 comments
-
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Beta Was this translation helpful? Give feedback.
-
This seems to be a C# language feature request, thus belong in https://github.com/dotnet/csharplang/discussions/. I didn't find a preexisting request for exactly this. #3464 is for an implicit conversion from an interface type, rather than conversion defined in an interface type from an unrelated type to a type parameter of the interface. The C# 8 default interface methods speclet explicitly ruled out conversion operators as static interface members. That text was added in #2967, in November 2019. I didn't find anything related from the November 2019 language design meeting notes, but #406 mentions conversion operators and points to C# Language Design Notes for Jun 27, 2017, where this was apparently decided. From the metadata viewpoint, the conversion operator would not be any more difficult than static interface methods in general. For the CLR, it would be just a static method call. There could be difficulties in the C# language specification, compiler implementation, and IDE support. |
Beta Was this translation helpful? Give feedback.
-
This may be supported by #5497 public interface IData<T> { ... }
public implcit extension UserExtension<T> for T where T : IData<T>
{
public static implicit operator T(int id)
{
}
}
public class User : IData<User>
{
}
User user = 1; |
Beta Was this translation helpful? Give feedback.
-
How is the interface supposed to create an instance of an unknown type? It cannot call any constructor. If it's not calling constructors (e.g., get an instance from another type's generic static method), it's probably better to provide it as a static method instead. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I want the interface to implement implicit arithmetic overloading and apply it to the implementation class.
Beta Was this translation helpful? Give feedback.
All reactions