really tenacious guy

First Vala Steps

Network connectivity exists, 2Kb/s or even worse. Still, got plenty of time to try a new GObject based language called Vala. It’s syntax is heavily C# based (to make developers switch from evil managed memory hog Mono, I guess). So I tried it. And I liked it. Here’s my list of puzzle answers:

Properties

Vala’s properties are not completely C# like, in contrast you cannot create setters/getters that make computations using the name of the property. Consider the following example:
namespace Test {
    class Property {

        public int temperature {
            get {
                return temperature;
            }
            set {
                this.temperature = value;
            }
        }

        public int run() {
            temperature = 50;
            stdout.printf("temperature: %d\n", temperature);
            return 0;
        }

        public static int main() {
            var app = new Property();
            return app.run();
        }
    }
}
It will compile fine, but when started,
Segmentation fault
Will be produced. It happens because the generated C code contains an infinite loop. Which fails due to too deep nested level:
 static gint test_property_run (TestProperty* self) {
         g_return_val_if_fail (self != NULL, 0);
         test_property_set_temperature (self, 50);
         fprintf (stdout, "temperature: %d\n", test_property_get_temperature (self));
         return 0;
 }
 ...
 static gint test_property_get_temperature (TestProperty* self) {
         g_return_val_if_fail (self != NULL, 0);
→        return test_property_get_temperature (self);
 }
Creating a new _temperature member and setting up temperature property will produce the correct result:
private int _temperature;

public int temperature {
  get {
    return _temperature;
  }
  set {
     this._temperature = value;
  }
}

HashTable

Another strange segfault I was getting in the following code:
namespace HashTableTest {
    class Application {

        public static int main(string[] args) {
            HashTable<int, int> ht =  new HashTable<int, int>(
                    int_hash,
                    int_equal
            );

            int key = 100;
            int h_value = 256;

            ht.insert(key, h_value);

            stdout.printf("Value: %d\n", ht.lookup(key));

            return 0;
        }
    }
}
Looks nice, but it will not work. If you examine the generated code, you will find that g_hash_table_insert is trying to cast GINT_TO_POINTER() on key. GINT_TO_POINTER(int) is actually a #define to (gpointer)(int) with gpointer as a #define to void *. So the key behaves as *(void *)100. Not good. The reason is that As GHashTable only supports pointer-based keys and values, you have to use a boxed GValue.. In our case we need to make key to be a reference type. The quick and correct solution is to modify the constructor as follows:
            HashTable<int?, Value> ht =  new HashTable<int?, Value>(
Actually, value of int? should be OK, but the resulting C code does not compile, value of int type will work but it will be incorrect from the perspective of data integrity. More things to come

Comments