Saturday 31 October 2020

Top and Bottom Type

When reading this post by Axel Rauschmayer about any being a "top type" in TypeScript, I found particularly interesting one of the comments, that argues that it's both a top and a bottom type:

1. any is like a top type in that you can assign values of all other types to it: let a: any = x works whatever type x has.
2. any is like a bottom type in that you can assign values with an any type to all other types: let a: any; let x: T = a is allowed for all types T (except never).

This made me revisit the behaviour of the dynamic keyword in C#. It's clear that it behaves as a top type, but I was a bit dubious about how the compiler dealed with it and to what extent it could be also considered a bottom type. Some code to the rescue:



    interface ITalkative
    {
        string SayHi();
    }
    
    class Person: ITalkative
    {
        public string SayHi()
        {
            return "Bonjour";
        }
    }

    class Printer
    {
        public static void Print(ITalkative t)
        {
            Console.WriteLine("[" + t.SayHi() + "]");
        }
    }

    class Cat
    {
        public string SayHi()
        {
            return "Miau";
        }
    }

 
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Started");
            dynamic ob = new Cat();

            try{
                Printer.Print(ob);
            }
            catch(Exception ex){
                Console.WriteLine("1. " + ex.Message);
                //The best overloaded method match for 'Dynamic.Printer.Print(Dynamic.ITalkative)' has some invalid arguments
            }
            
            try{
                Person p = ob;
            }
            catch(Exception ex){
                Console.WriteLine("2. " + ex.Message);
                //Cannot implicitly convert type 'Dynamic.Cat' to 'Dynamic.Person'
            }

            try{
                Printer.Print(ob as Person); //casting returns null and then Print fails
            }
            catch(Exception ex){
                Console.WriteLine("3. " + ex.Message);
                //Object reference not set to an instance of an object.
            }

            try{
                Person p1 = ob as Person;
            }
            catch(Exception ex){
                Console.WriteLine("4. " + ex.Message);
            }

  
        }
    }

I'm particularly interested in the first case. I was not sure if the compiler would allow that, but it does. So the compiler allows me to invoke a method that expects ITalkative with just a "dynamic" value, so yes, we can say that dynamic is a bottom type. Notice however that dynamic in C# has nothing to do with Duck Typing (or TypeScript's structural typing), so it's obvious that this will fail at runtime. The method invocation Printer.Print fails cause the runtime checks (it's different from a cast error) if the parameter implements ITalkative, as that is not the case, we get a The best overloaded method match for 'Dynamic.Printer.Print(Dynamic.ITalkative)' has some invalid arguments exception.

A decade ago (puff...) I had posted about Duck Typing in C# and dynamic, but probably I was thinking that we would already get a compiler error in this situation, rather than a runtime one.

Tuesday 13 October 2020

Sin Fin

Sin Fin is a beautiful Spanish film. It combines Drama, Romance and Science Fiction. The Science Fiction element (time travelling from 2015 to 1993) is there just to allow the story to happen, the romance part leads to the drama, that is the prevailing element.

A story about how the purchase of a dream brings about the destruction of who has been devoted to you for 20 years, about devoting the rest of your broken life so that the materialization of the dream can erase the nightmare, about correcting in 1 day the errors of 2 decades...

I have a particular appreciation for any art piece (whatever the form) that portrays the passage of time, distant moments of one life, that reminds us of our temporarity, that confronts us to how what we do today determines our future, and how maybe one day in that future we'll look at that distant "today" with sorrow and tears or with joy and reverence, with a desire to erase or repeat that day. This film is about making mistakes and being given the chance to travel in time to correct those mistakes, to save a life, to save two lives.

The story stars in 1993, with a boy and a girl of around 20, in that moment in your existence where you have that sacred power to dream and shape your future, where a single day can determine the joys and pains of the ensuing decades. It's such a power and such a responsibility that it gives me vertigo now. I was not aware of that power when I had it, it was not until a couple of years ago that I started to look back at that time in my life with a sort of nostalgia, but a healthy one, with a sort of gratitude for still having a present now, and for not being so fully aware of how each decision at that time was drawing a path for which there would not be way back...

Twenty years later the energetic, dreamy girl has turned into a depressive adult with no other prospect that putting an end to everything. The genious guy obsessed with creating something "big" is now an unhappy adult with that same obsession that so far has not done other thing than destroying their lives... I'll stop here, it should have been enough to wake up your interest or to bore you even more than usual.

It's a slightly rainy autumn afternoon in Toulouse, a vacation day in the first year of the pandemia. I'm listening to Yarostan and Les deux minutes de la Haine, 2 bands that I already knew, but that did not fully get my attention until I listened to their stuff in the best record of this year?... and I'm giving these details beacause I think they play well with the melancholy in this post, and if some day in the future I read back these paragraphs I'll enjoy being reminded of these details, details that make up a life...

Saturday 3 October 2020

Defend Armenia

I'm not religious, but as a proud European I clearly adhere to Eric Zemmour's mantra about the roots of European civilization: "Europe is the Greek Culture, the Roman Culture and Christianism". Yes, we have the Celts, the Germanic tribes... but 90% of what we are as Europeans come from those 3 cultural and moral frameworks.

Armenia, (by the way, the first Christian nation) is part of our Western civilization, as obviously, Greece is. Last month Turkey (the reverese reflection of everything European Civilization stands for, our eternal enemy) was threatening Greece with war as Turkey intends to steal part of Greece's maritime space to leverage the gas fields that are supposed to be there. Most of Europe said nothing, it was only France who clearly stood by our European Brothers. On the other side, Germany, terrified by the enormous amount of Turkish invaders that have settled on the country in the last 50 years and how they (particularly the neo-fascist, paramilitary organization grey wolves) could react setting the country in flames... did as much as possible to avoid any sort of common European action that could bother the neo-sultan Erdogan... 

This last week, Azerbaijan (this is a different country, but Azerbaijanis are a Turkic ethnic group, so they are not much different from the Turkish (scum) people, by the way, obviously both of them are Muslims) has launched an attack on the Armenian people living in the Nagorno Karabakh region. Almost immediatelly Erdogan expressed his support for his ethnic, muslim brothers and made it clear that he would help them by any means.

Turks have a tradition of killing (and torturing, and enslaving, and raping and whatever...) Christians, particularly Armenians and Greeks (but also Serbians, Romanians... well any sort of Europeans... haven't you heard about the Ottoman pirates?) so this new war against Armenia (as the low level conquest war that ultranationalist-islamist Turkish criminals fight everyday in the unfortunate European cities where they have settled). In the last decades the Turkish government had favored to kill and torture Kurds and now they aim to leverage the knowledge that they gained while massacring the Kurds in Northern Syria to slaughter the Armenians. At least, they are sending against the Armenians some of the same Jihadist mercenaries that they paid for to try to annihilate the Kurds. If you think I'm a delirious European nationalist... well, you can see that it's not just me, it's Emmanuel Macron also who says it.


 

In the last decades, cowardice, self-hate and the absolute ignorance of almost any lesson drawn from our history... have fed the decadence of European civilization. Supporting our Armenian and Greek brothers (by any means necessary) against our common enemy should be a first step in regaining our dignity as a people and reconquering our lands.

I've found interesting this article about the influential Armenian diaspora getting ready to support the land of their ancestors. And well, this picture of a 106 years old Armenian woman during the previous chapter of this war (back in the 90's) is an incarnation of courage and dignity.