11 ms·
Extension methods in C# do the same thing and have been around, what 5ish years now? Since .Net 3.5. Here's an example implementation: public static class
by mattmanser 13y ago
Extension methods in C# do the same thing and have been around, what 5ish years now? Since .Net 3.5.
Here's an example implementation:
public static class DateExtension
{
public static TimeSpan Days(this int daysCount)
{
return new TimeSpan(daysCount, 0, 0, 0); //days, hours, mins, secs for those wondering
}
public static DateTime FromNow(this TimeSpan addTime)
{
return DateTime.Now.Add(addTime);
}
}
usage is the same as Rust:
2.Days().FromNow();
Basically if this impresses you, you really should give modern C# a whirl as there's a lot more you can do than that.
Much of how it does stuff now is how my perfect language would do it. There are bits of Ruby I love, and Rust is looking more exciting to me than Go, but the C# team really have done some amazing work.
- steveklabnik 13y agoI am certainly not implying that Rust is the only language that supports this kind of thing. Haskell is where I first saw some of this. I'm just happy that Rust is doing it well. C# is a great language, but I don't use Microsoft technology, and it's effectively Windows only.
- majorsc2noob 13y agoI run C# in production on Mac OSX, IOS and Android phones. What do you mean by Windows only?
- steveklabnik 13y agoIf you work hard enough, you can do anything. Mono is slow, doesn't support everything, and most of the help you'll get is Windows specific.
- majorsc2noob 13y agoThat is just FUD im my opinion. I havent har any issues with getting my applications working on multiple platforms. Have you har performance issues with C# on IOS?
- mattmanser 13y agoBe practical. I've had to support wordpress on IIS before, it's always a lot of hassle not using the tech in its native environment. Same with MySQL + .Net. The EF, for example, played really poorly with MySQL. I can't imagine using mono is for the faint hearted. For example in the new MS programming language thread [1] today someone mentioned they ended up giving up on the .Net GUI controls and using GTK# instead because it was so unreliable. Another mentioned that the transition from one SQL driver to another had left a lot of projects hanging in the wind with SQL breaking on mono but working on Windows. [1] https://news.ycombinator.com/item?id=6974494 https://news.ycombinator.com/item?id=6974494
- mfn 13y agoI don't think that's a fair comparison. Rust doesn't come with a set of functioning, well-tested and cross-platform GUI libraries and database drivers either. Running C# the language with the BCL on mono is pretty painless, and offers a ton of functionality.
- iv_08 13y agoYou're using Rust and call Mono incomplete? lololololol
- catnaroek 13y agoOne problem with extension methods is that you cannot statically express the idea that multiple classes implement the same extension methods. In particular, you cannot make a generic whose type parameter is guaranteed to implement a given set of extension methods. This, in my opinion, reduces the usefulness of extension methods from "potential game changer" to "mere syntactic sugar".
- bad_user 13y agoYou mean type-classes? Scala and Haskel have them. It's certainly nice to be able to do statically type-checked stuff like: list.sorted list.max list.sum The problems with languages like C# (or Java) go beyond the lack of type-classes or similar. For example, given a type such as List<T>, you're restricted to define methods only "for all T", as these languages don't allow you to define methods only "for some T". Examples include methods such as "sorted" or "max", relying on type T implement some sort of Ordering interface, or "sum", relying on type T to be some sort of Number. So even if you would have a way to specify that types X, Y and Z have implementations for this and that set of extension methods, it would still be of somewhat limited use, given that one wouldn't be able to also use this in code making heavy use of generics, much like how interfaces themselves aren't used much in code making use of generics, because code making use of generics is usually some dumb Container<T>, where almost nothing about T is known. The best part about type classes is that you don't have to control/modify the type's implementation, in order to make it implement a certain type-class. Type-classes are like open interfaces. If the builders of C# never bothered to provide you with some common interface you want for built-in types, that's OK, because you can make one up. Scala also has implicit conversions and "view bounds". Implicit conversions go beyond extension methods and View Bounds allow one to specify that some type X can be viewed as some type Y (either Y inherits from X, or there exists an implicit conversion from X to Y). Something like: def sample[X <% Y](item: X) = item.callMethodFromY But these are mostly useful for builtins and deprecated for general-purpose usage in favor of context bounds (type-classes). It's still nice that builtins such as Array[T] is viewable as a Seq[T] and Strings are viewable (in true FP tradition) as sequences of chars, allowing one to do this: "some string".map(_.toUpper) // => "SOME STRING" What's interesting about the above example is that mapping over a string, produces a string, highlighting another Scala feature that goes beyond type-classes: "some string".map(_.toString) // => Seq(s, o, m, e, " ", s, t, r, i, n, g) This is the CanBuildFrom pattern. See this awesome example from M. Odersky himself: http://stackoverflow.com/a/1728140 http://stackoverflow.com/a/1728140
- samirahmed 13y agoExtension methods are a godsent in C#. Really allow you to write extremely expressive code. I cannot agree enough. This is not new for any C# dev. Expressiveness is not as great as ruby, but certainly on par with Rust LINQ extension methods by Jon Skeet - https://code.google.com/p/morelinq/ https://code.google.com/p/morelinq/ I would recommend reading this stackoverflow post to see some great extension methods. http://stackoverflow.com/questions/271398/what-are-your-favorite-extension-methods-for-c-codeplex-com-extensionoverflow http://stackoverflow.com/questions/271398/what-are-your-favo...