Re: [Vala] [Genie] static dict



On Fri, Jan 29, 2010 at 17:03:06 -0500, Arc Riley wrote:
struct rgba
  r : float
  g : float
  b : float
  a : float

class Color : Object
  _map : static dict of string,rgba = new dict of string,rgba
  _map["white"] = {1.0f, 1.0f, 1.0f, 1.0f}
  _map["red"] = {1.0f, 0.0f, 0.0f, 1.0f}
  _map["green"] = {0.0f, 1.0f, 0.0f, 1.0f}
  _map["blue"] = {0.0f, 0.0f, 1.0f, 1.0f}
  _map["black"] = {0.0f, 0.0f, 0.0f, 1.0f}

This code doesn't work.

Telling us it does not work really does not help diagnosing the problem. You
should have mentioned the errors you saw.

In fact, there were so many of them (the obvious mistake of writing
statements directly to a class definition being the most obvious one).

So:

- The struct rgba is declared correctly.
- To recognize dict, you need to compile with --pkg=gee-1.0
- Structs can't be used as generic arguments, because the generic type is
  really compiled just once simply using void *. Therefore rgba needs to be
  boxed. The dict must be "dict of string,rgba?"
- Now the declaration of _map is right, but the initialization is not.
- The statements filling in _map may not live in the class itself. You need
  to get them into the static constructor.
- Now unfortunately I didn't find how to declare a class init (static
  construct works in vala, but neither static init nor class init seem to
  work in geine), but putting them in a function and using that as
  initializer for the variable did the trick. So your code should look like:


        struct rgba
                r : float
                g : float
                b : float
                a : float

        class Color : Object
                def static mkmap() : dict of string,rgba?
                        var map = new dict of string,rgba?
                        map["white"] = {1.0f, 1.0f, 1.0f, 1.0f}
                        map["red"] = {1.0f, 0.0f, 0.0f, 1.0f}
                        map["green"] = {0.0f, 1.0f, 0.0f, 1.0f}
                        map["blue"] = {0.0f, 0.0f, 1.0f, 1.0f}
                        map["black"] = {0.0f, 0.0f, 0.0f, 1.0f}
                        return map

                _map : static dict of string,rgba? = mkmap()

Except, well, it does not work, because vala (0.7.9) generates invalid C code
for the boxed struct initializations. You can either go with making rgba
a class (which means you need to give it a constructor and stuff) or check
with latest master and if it still does not work there, report it as a bug.

-- 
                                                 Jan 'Bulb' Hudec <bulb ucw cz>



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]