In C# 4.0, we can annotate generic type parameters without
andin
annotations to specify whether they should behavecovariantly orcontravariantly. This is mainlyuseful when using already defined standard interfaces. Covariance means that you can useIEnumerable<string>
in place whereIEnumerable<object>
is expected. Contravarianceallows you to passIComparable<object>
as an argument of a method takingIComparable<string>
.
So far, so good. If you already learned about covariance and contravariance in C# 4, then the above two examples are probably familiar. If you're new to the concepts, then the examples should make sense (after a bit of thinking, but I'll say more about them). However, there is still a number of questions. Is there some easy way to explain the two concepts? Why one option makes sense for some types and the other for different types? And why the hell is it calledcovariance andcontravariance anyway?
In this blog post, I'll explain some of the mathematics that you can use to think about covariance and contravariance.
Published:Tuesday, 19 June 2012, 2:24 PM
Tags:c#,research
Read the complete article
Over the last year, I wrote quite a lot of articles about agent-based programming in F#.Agents (inspired by Erlang) provide a great abstraction for writing concurrent and scalablesystems. They are a great fit for both server-side development (for example, handling a large number of concurrent requests), but also for user interface (for example, keepingstate in an application with background tasks and interactive interface).
When writing reusable agents, we usually encapsulate agent in an F# object type. The type provides methods for sending messages to the agent. However, sometimes the agentalso needs to report some state change that can be handled by another interested agent.This is done using F# events. However, F# events do not specify threading behaviour, so there is a number of options.
In this article (inspired by a recent email discussion), I describe three ways of reporting events from an agent. The options differ in what thread is used to report theevent. Choosing the right option is important as it affects scalability and simplicityof your agent-based code.
Published:Saturday, 16 June 2012, 12:23 AM
Tags:f#,asynchronous
Read the complete article