Search code examples
vala

Accessing static class variable in foreach context in vala


I originally wanted to use static in method but, since it does not exists in vala, i'm trying to have a static object.

The only problem is that i got this error

utils.vala:112.11-112.57: error: invocation not supported in this context
      Synapse.Utils.spec_char_map (_chrs[0], _chrs[1]);
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

For above code

    namespace Synapse
    {
      namespace Utils
      {
        static Gee.HashMap spec_char_map;

        public static void map_special_chars (ref string query) {
          if (null == spec_char_map)
          {
            message("INIIIIT");
            spec_char_map = new Gee.HashMap<char, char> ();
            var spec_char_table = "ъ=-,Ь=-,Ъ=-,ь=-";

            foreach (var spec_char in spec_char_table.split (","))
            {
              var _chrs = spec_char.split ("=");
              spec_char_map (_chrs[0], _chrs[1]);
            }
          }
        }
      }
    }

What is this error for, why I can't make use of my Hashtable in the foreach context ? Is there a simpler solution for what i want to do ?


Solution

  • This is not the correct syntax to put something in a hashmap. Use:

    spec_char_map.set (_chrs[0], _chrs[1]);
    

    or

    spec_char_map[_chrs[0]] = _chrs[1];
    

    Also, consider using GLib.Once to create a thread-safe initialiser.