Sunday, 29 June 2014

C# 6 Dictionary Initializers

I already wrote back in December how little excited I was about the (at that time not confirmed) new features in C#. I've confirmed that feeling over the last months (well, at least it seems like the Safe Navigation Operator (called Null Propagation here) is finally making it into the language). I find most of the features (setting Roslyn aside) add little value (and sometimes add confusion).

One of the features really got me confused, but hopefully other developers found it weird too, and Eric Lippert gave a clear explanation in StackOverflow, that once again made it evident how important it is to understand what the compiler is doing under the covers (well, in this case it was not a matter of decompiling anything, but a matter of having taken the time to fully read the documentation).

So, C# 6 is adding Dictionary Initializers, that is:

 var departments = new Dictionary<String, int> {
["Haute-Garonne"] = 31, 
["Gironde"] = 33 

It gave me sort of a Deja Vu feeling, don't we already have this? In C# we've been able to write this since Object and Collection Initializers were introduced along with Linq:

var departments = new Dictionary<String, int> {
{"Gironde", 33}

So, are we duplicating syntax for doing just the same? The answer is that for this particular type, System.Collections.Generic.Dictionary, both syntaxes achieve the same (though using different code underneath), but there are classes for which the first syntax won't work and the second will do. Let's see:

In the second case, as I said, we're using a Collection Initializer to initialize a Dictionary. This means that our class has to implement IEnumerable, and has to contain an Add method. Under the covers the compiler will transfor these assignments into calls to Add.

In the first case, Dictionary Initializers, no Add method is used, but an Indexer. So in principle, any class featuring an Indexer can be initialized this way. This is obviously more powerful than Collection Initializers and really adds value to the language.

By the way, I've just seen that the proposed new syntax for string indexed members (obj.$name <=> obj["name"]) seems to have been withdrawn. Honestly I think this has been the right decision, that syntax didn't seem to add anything to me.

This is the original question in StackOverflow that helped me understand the whole thing.

I don't understand the dictionary initializer thing. The counterexamples that keep getting given are m = new Dictionary(); m[1] = 2; and so on. But we already have a real dictionary initializer syntax: new Dictionary { { 1, 2 }, { 3, 4 } }. Any idea why we needed another syntax for it? - Aaronaught Apr 6 at 15:13

@Aaronaught: Are you guaranteed that all dictionary objects have an Add method that takes pairs? Suppose for example you have an object that represents an XML document where element["attr"] = "value" is legal. Do we have a guarantee that this object has a method Add that happens to take two strings and adds an attribute-value pair? It seems plausible that the author of that object neglected to implement such a method. – Eric Lippert Apr 6 at 15:18

Sunday, 22 June 2014

Kill Parent Process

Months ago I wrote about the differens between Windows and Linux with regards to how they handle Processes and Threads. There's another important difference regarding processes, the Parent-Child relationship.

I think we can say this Parent Process - Child Process relation in Linux is strong. There are many ways to see a process tree, and I used to think that killing a parent process automatically kills its children. Well, this is partially true. If you start a process from the terminal and you kill the terminal, the child process will be also killed (unless you handle the SIGUP signal), but this is not so when the parent process is a normal process

No. If the parent is killed, children become children of the init process (that has the process id 1 and is launched as the first user process by the kernel).

In windows, the only way to see a process tree that is known to me is using Process Explorer, and definitely, killing the command line won't kill the processes started from it.

The thing is that lately, when developing in Windowns, I've come across the need of programmatically killing a main process and all its children. I know the PID of the parent process, but just killing it will leave its children alive and well. Of course you can go to the Task Manager and select the "Kill Process Tree" option, but as I've said, I need to do this programmatically. There's not any sort of Win32 API method like "KillProcessTree". Indeed, you don't even have an out of the box GetParentProcessId function. As explained here, you could create your own and use it along with a list of all processes in the system. Not an immediate solution, and seems like error prone as Windows will reuse PIDs as they are released.

"Alternatively, can someone alleviate my fear that the pid I'm passing might belong to another process after some time if the parent has died?"
Yes, the PID can be reused. Unlike UNIX, Windows does not maintain a strong parent-child relationship tree.

Hopefully, there's a much more simple solution, thanks to one of those powerful Windows command line tools that most people seem unaware of (driverquery is another one that comes to mind).


You pass it the PID of a process et voila! the process will be killed along with all its children. A nice Process massacre.

It seems like it's also more simple than it initially seemed to get the PPID of a given PID, you can use the almighty (and barely known) WMI console:

wmic process where (processid=PROCID_HERE) get parentprocessid

I've mentioned above that Windows will reuse Process IDs once the corresponding process ceases to exist. This seems quite weird to me, I think it would make more sense to follow the Linux approach (from here and here

As new processes fork in, PIDs will increase to a system-dependent limit and then wrap around. The kernel will not reuse a PID before this wrap-around happens.

Under Unix, process IDs are usually allocated on a sequential basis, beginning at 0 and rising to a maximum value which varies from system to system. Once this limit is reached, allocation restarts at zero and again increases. However, for this and subsequent passes any PIDs still assigned to processes are skipped.

Saturday, 21 June 2014

Paris, Berlin, rail tracks and more

Murals covering the whole wall of a building are the most astonishing side of street art for me. Berlin is full of them and is probably unbeatable (as with any other element of the Street Art family).
At first sight, Street Art is far less present and interesting in Paris than in Berlin (notice that I know Berlin much better than Paris, and I don't know if there are any neighborhoods in Paris that could be considered some sort of equivalent to Kreuzberg, Friedrichshain or Prenzlauer Berg).

Anyway, in my last visit to the so beautiful capital of "La France", I came across 2 pretty good murals, I'll share some pics here:

This one located in Boulevard Magenta

And this one located just across one corner of the Centre Pompidou.

By the way, I've got some mixed feelings about the Pompidou building. I tend to love this kind of High Tech architecture, but there's something with this piece that does not completely play with me, maybe it's just the location, it would a quite better fit for La Defénse than for "classical Paris".

One element of urban landscapes that really delights me is the presence of elevated metro-train-tram tracks across the city. Be it sections of the DLR in London, the U-Bahn/S-Bahn in Vienna, Hamburg or Berlin. The most outstanding of these for me are located in Berlin, in particular the section of the U-Bahn crossing Kreuzberg between Schleisches Tor and Hallesches Tor, and the section of the S-Bahn crossing central Berlin (when it goes through the Museum Insel, wow, that's crazy for me). Paris sports a very beautiful and similar Metro section. Line 2 between Anvers and Stalingrad (not sure if after this stop it continues overground or not) will make you feel as if you had been transported into Kreuzberg (change the people of Turkish ascent for people of Maghrebian or Indian ascent). As a plus, from La Chapelle you'll have superb views of the train tracks going into Gare du Nord.

Train tracks, I've got sort of an obsession with them. A wide set of rusty trail tracks surrounded by dirty graffited walls and the view at the end of a large and old European train station is a quintessential representation of beauty for me. I guess it's normal bearing in mind my taste for the "Beauty in dereliction" thing. Paris does not have a single Main train station (Hauptbahnhof), but like 6 "big ones". Sure this can be a real pain for travelers having to switch from one to another, but if you happen to be into this kind of views that I've depicted, it'll mean much joy for your eyes. The views over Gare du Nord and Gare de L'Est are astonishing, but maybe the views over St Lazare from the intersection of Bd de Batignoles and Rue du Rome are probably the best.

By the way, if you happen to be into this kind of stuff and you're lucky to be in Toulouse, notice that Metro Line A goes overground for a short length in sections between Jolimont and Roseraie, and between Argoulets and Balma Gramont in the north part, and between Bagatelle and Mirai to the south. It does so by means of some interesting bridges spanning over the Péripherique.

Saturday, 14 June 2014

The City of Light

Paris is commonly referred to as "La Ville-Lumière" ("The City of Light"), as wikipedia explains:

The name may come from its reputation as a centre of education and ideas during the Age of Enlightenment. The name took on a more literal sense when Paris became one of the first European cities to adopt gas street lighting: the Passage des Panoramas was Paris's first gas-lit throughfare from 1817.[11] Beginning in the 1860s, Napoleon III had the boulevards and streets of Paris illuminated by fifty-six thousand gas lamps, and the Arc de Triomphe, the Hôtel de Ville and Champs-Élysées were decorated with garlands of lights.

In my last visit to this gorgeous city, I was surprised by a dry storm that lasted for almost 3 hours (it finally started to rain when I was happily about to go to sleep). The storm displayed an astonishing amount of lightning and after quite a few failing attempts to capture some of this with my camera, I was lucky to get this picture:

NotreDame under a furious heaven. Sure it won't gain me a prize, but I pretty much like it :-). And well, I'll also share this one: