Replies: 10 comments
-
Isn't the solution to actually change the methods into extension methods? Have you considered proposing that in corefx? |
Beta Was this translation helpful? Give feedback.
-
@svick no, I suppose that turning all methods into |
Beta Was this translation helpful? Give feedback.
-
that's simply a breaking change. Aside that namespace System {
public static class X {
// we made this an extension method in corefx
public static bool M(this string str) { .. }
}
}
namespace MyNamespace {
public static class Y {
// have already been an extension method
public static bool M(this string str) { .. }
}
}
// then this code will break
using System;
using MyNamespace;
class C {
void M()
{
"".M(); // ERROR: call is ambigious
}
} I liked this proposal but it would make more sense if it is introduced at the same time that we have "extension everything" feature. |
Beta Was this translation helpful? Give feedback.
-
However, we could define those extensions in another namespace like System.Extensions, if that's what you meant. Though I don't think that's a good idea since it duplicates some of methods. PS: Making string.IsNullOrEmpty an extension method requires another feature because String is not static. |
Beta Was this translation helpful? Give feedback.
-
@alrz I don't see how it differs from today's situation when you have two similar extension methods.
This proposal doesn't require class to be static. Since string.IsNullOrEmpty is a static method which takes its instance as first parameter it can be treated as an extension. |
Beta Was this translation helpful? Give feedback.
-
If you already defined an extension method named
If you're proposing to just make string.IsNullOrEmpty an extension, that wouldn't be possible because currently the compiler requires the containing class to be static. |
Beta Was this translation helpful? Give feedback.
-
You can now declare two static classes with
|
Beta Was this translation helpful? Give feedback.
-
Note: none of my claims are applied to |
Beta Was this translation helpful? Give feedback.
-
@alrz ah, I missed it. Then ok. But I explicitely wrote that we don't require any CLR support so it can be C#-only feature. Everyone likes this kind of features because they can use it on .Net 2.0 and don't require anything but latest visual studio. |
Beta Was this translation helpful? Give feedback.
-
Today encountered with yet another situation where this is needed. int entityId = input.AsMaybe().Map(long.Parse).GetOrElse(-1); It's not possible now to rewrite it on null propagation. And I don't see how could it be rewrited properly without boilerplate However, if we don't import two or more types with using extension System.Int64;
...
int entityId = input?.Parse() ?? -1; |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Problem
We often encounter a problem with methods such as
string.IsNullOrEmpty
,char.IsLetterOrDigit
that could be extension but aren't. I see a lot of project full of:It could be solved with
using static
, but it doen't work well with null propagation:but things become worse with reference types:
I has written here a typical example: we have some query with null propagations and then we want to call some static method. We always can follow a good ol' way by storing query into variable and then checking it before calling some method (
Array.Exists
in this example), but why have we if we already have compiler for that purpose?Solution
In addition to
using static
addusing extension
that just allow to call all public static class methods that takes instance of this or derived class as first parameter as extensions.that compiles just like null propagation:
Beta Was this translation helpful? Give feedback.
All reactions