-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider allowing implicit operators in a throw statement #9971
Comments
To note, Swift's syntax isn't because it's exception types are any different, it's because Swift's enums are actually classes. You'll notice that it's illegal to throw a class or enum value in Swift unless it inherits from I personally don't see much issue in allowing However, the way you have that implicit conversion I think would make that really complicated and confusing. When performing an implicit cast the C# knows exactly which type the expression is being converted from and converted to. This limits the scope of types in which the compiler will scan for potential conversion operators. With your use case above the compiler would only have half of that information. The compiler would be required to scan literally every referenced class that derives from TL;DR, assuming it wouldn't introduce other ambiguities in syntax, I would be all for allowing for implicit conversions using operators defined in the source type. However given that the compiler would have no idea which type of exception should be thrown that it doesn't make sense to have the compiler support an implicit operator defined in the target |
@HaloFour thanks for the comments I don't actually propose a swift like system, I'm just saying that if C# supports implicit conversion of types, then we should be able to throw an implicitly converted exception |
Yes, I just wanted to point out that difference. Although #6739 might bring that syntax into C#.
Sure, and I somewhat agree. If you had an implicit conversion operator declared in your However, in your use case there's nothing to hint to the C# compiler that what you want is a This issue isn't unique to public class Foo { }
public class Bar : Foo {
public static implicit operator Bar(string value) {
return new Bar();
}
}
static class Program {
static void Main() {
Foo foo = "Hello"; // CS0029: Cannot implicitly convert type 'string' to 'Foo'
Bar bar = "World"; // just fine
}
} |
As has been noted, the proposed change would not affect the use case in the original issue. The original use case has no user-defined conversion from class Program
{
static void Main(string[] args)
{
Foo foo = null;
throw foo;
}
}
class Foo
{
public static implicit operator Exception(Foo self) { return null; }
} |
Example problem
The above is quite a nice way of throwing an error, and is quite descriptive as it provides context to what you're throwing.
However, this doesn't work, due to the error above.
Proposition
Modify the type checking and generation of the throw statement to implicitly convert a type to Exception if it is possible.
This behaviour is quite similar to Swift, I think.
Currently, the alternative is
Unfortunately, this is ugly and doesn't offer any advantage from the implicit cast
The text was updated successfully, but these errors were encountered: