by mattmanser · 12y ago · 0 comments
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.
C# is a great language, but I don't use Microsoft technology, and it's effectively Windows only.
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/1728140Yep.
> Scala and Haskel have them.
Correction: Haskell has them, Rust has a limited form of them, and Scala encodes them using a horrible hack.
> 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", relying on type T to be part of the Ordering type-class, or "max", relying on type T to be part of the Number type-class.
Both C# and Java have bounded generics. The problem is that bounded quantification is fundamentally tied to class/interface hierarchies.
> 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.
I know.
> Scala also has implicit conversions and "view bounds".
To be honest, I am not fond of those. (And, in general, I am not fond of the way Scala does things.) If I want to view an X as a Y, a regular function "f : X -> Y" can already do that. The notion of subtyping roughly corresponds to the idea that certain functions of the form "upcast : Subtype -> Supertype" are worth implicitly applying. I do not agree with this idea.
You can view it like that, my perspective is that in Scala the underlying mechanism (passing the dictionary around by means of implicit parameters) is more explicit. Much like how prototypes in Javascript work, versus class-based OOP. With prototypes you can have class-based design, but you can also go beyond that. And this is similar to how Scala deals with type-classes. This does have a cost in terms of learning curve, but then you've got extra flexibility. CanBuildFrom is not a type-class.
On implicit conversions and view bounds, I'm not fond of them either. I actually hope that they'll go away completely. I think they made it in the language (and survive), only because Scala runs on the JVM and they had to make the builtins (like Arrays, Strings) behave nicely without much overhead. In Haskell, Strings really are lists of chars. In Scala you can't do it, unless they introduced their own String class, which would have been awful, or if they introduced some sort of implicit conversion mechanism, which they did. And this could have been some magic that Scala's compiler applies for builtins, but personally I dislike compiler magic (that you can't hook into) more than I dislike hacks made for interoperability (if only they got rid of that implicit conversion from Any to String in Predef, which is so freaking annoying).
Bounded generics do not solve the problem I tried to express. Here's a Scala sample:
case class Container[T](elems: T*) {
def max(implicit ev: T <:< Comparable[T]) =
elems.reduceLeft((a, b) => {
if (a.compareTo(b) > 1) a else b
})
}
Comparable is not a type-class, it's the standard java.lang.Comparable interface. Yet the defined method, "max", only works if the container's generic T implements Comparable[T]. As in ... scala> Container("1", "3", "2").max
res: String = 3
Of course, Java's standard interfaces are normally useless, so Integers don't implement Comparable ... scala> Container(1, 3, 2).max
<console>:10: error: Cannot prove that Int <:< Comparable[Int].
Container(1, 3, 2).max
^
Note that this restriction is only on max() and not on the generic class-wide definition of T. In Java and C#, you can only do this: class Container<T extends Comparable<T>>
/* ... */
Oh, but look, now the whole container can only be used for things that are Comparable and nothing else.In retrospect, sort of a horrible idea, but I wonder, is there a type system out there expressive enough to allow that behavior?
After a few moments thought I realized why it wouldn't be possible in languages that use a fixed v-table, but it should be possible in languages that use more flexible calling techniques!
Or am I thinking about this too much and this is basically what type classes would buy me?
I think that's easily possible in Scala. I guess one wouldn't even need typeclasses for that.
trait InterfaceA
trait InterfaceB { self: InterfaceA => }
class A extends InterfaceA // compiles
class B extends InterfaceB // does not compile
class B extends InterfaceA with InterfaceB // compiles
"self" isn't a keyword, you can call it whatever you like.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/
I would recommend reading this stackoverflow post to see some great extension methods.
http://stackoverflow.com/questions/271398/what-are-your-favo...