-
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
Extension operators #4945
Comments
+1 to that. |
@orthoxerox I would raise an issue in https://github.com/dotnet/corefx/issues as well. |
@dsaf, it's already there in coreclr, since System.String is in mscorlib: https://github.com/dotnet/coreclr/issues/542 |
I very much like the idea of being able to write extension operators. This would be particularly useful for writing DSLs. I think you should expand this proposal to include additional scenarios that demonstrate it's usefulness as the type casting example is not particularly strong. In particular, a generic extension operator. I have one gripe with your proposed syntax. Specifically, I think that the |
@aluanhaddad A few day ago I posted an example of using an extension operator for specializing generics that uses the |
This would allow to use types themselves as patterns which cause a conversion, e.g. static class Extensions {
public static explicit operator int?(string value) {
return int.TryParse(value, out var result) ? result : null;
}
}
// assuming a non-static `bool operator is(string, out string[])` for `Regex`
Regex regex = new Regex("...");
if(str is regex({int value})) {
// ...
}
// translates to
if(regex.op_Match(str, out string[] captures)) {
if(captures.Length == 1) {
var temp = Extensions.op_Explicit(captures[0]);
if (temp.HasValue) {
var value = temp.GetValueOrDefault();
// ...
}
}
} |
No, it would not. Pattern-matching does not apply user-defined conversions. If it did, it would use the implicit ones only. Also, there is no "array pattern". |
@alrz The only implicit user-defined conversion that would apply in any pattern-matching-like context is the conversion from the switch-expression of a switch-statement to a type that was switchable in C# 6: an enum, integral type, string, etc. We do not apply user-defined conversions in any other pattern-matching context. |
@gafter Is there a specific reason for this? Wouldn't it be more consistent if "it was allowed for all the types that is switchable in C#?". This statement is still true for C# 6.0 -- it applies the conversion to all the possible types for |
@alrz Those are the types that can have constant expressions (except float, double, and decimal). |
@gafter Ok, if C# had support for extension static class StringExtensions {
public static bool operator is(this string str, out int value) =>
int.TryParse(str, out value);
}
if(str is regex({ string(int value) }))
// and if we could omit known types in positional patterns
if(str is regex({ (int value) }))
if(regex.op_Match(str, out string[] captures)) {
if(captures.Length == 1) {
if(StringExtensions.op_Match(captures[0], out int value)) {
// ...
}
}
} That would be just perfect. |
Yes, obviously pattern-matching on something of type |
@gafter I am sure you saw my point though. I've defined an Basically |
@alrz I think you're imagining that pattern-matching does overload resolution on all of the possible |
@gafter So |
You write a class that names each pattern you're trying to define, and you place the pattern-matching operation there. And then you use that name in the pattern so it is clear to the reader which of them is being used. |
@gafter So that would be something like this: class Integer {
public static bool operator is(string str, out int value) => int.TryParse(out value);
// `Integer` vs `string` ------^
}
if(str is Integer(var value)) I suppose Now, if I want to define it as an extension operator for the type static class IntegerExtensions {
public static bool operator is(this string str, out int value) => ... ;
} It would not be clear that which type we are targeting. static class IntegerExtensions {
public static bool GetValues(this string str, out int value) => ... ;
} This is still an extension on type I think this needs something like #6136 to be clear about the intended target type. extension Int32 {
public static bool operator is(string str, out int value) => ... ;
} I'm not aware of any plans for supporting extension |
Issue moved to dotnet/csharplang #515 via ZenHub |
Feature request, Language C#
I would love to see more extension like stuff in C#. In this case - extension operators. It would be great to have ability to implement operators outside theirs' class or in other projects. Something like:
so
var viewModel = (ViewModel)model;
code would be compilable likeexplicit operator
was implemented insideViewModel
class.The text was updated successfully, but these errors were encountered: