Re: [Vala] help with java to vala



On 10/25/2012 09:39 AM, marcin saepia net
wrote:
Have you tried

 static construct {
      ...
    }

?

(see https://live.gnome.org/Vala/Tutorial)

m.

2012/10/23 bsquared <bwcode4u gmail com>:
Hello,

Is it possible to initialize a static const HashSet?
Similar to this from Java?

8< ------------------------------------------------
    private static final Set<String> STATIC_SET;

    static {
        STATIC_SET = new HashSet<String>();
        STATIC_SET.add("Data One");
        STATIC_SET.add("Data Two");
    }
  ------------------------------------------------- >8

Thank you.
--
Regards,
Brian

_______________________________________________
vala-list mailing list
vala-list gnome org
https://mail.gnome.org/mailman/listinfo/vala-list

Thanks for your response.  I was curious if the static constructor was
the right solution.

I attempted a simple test using this:
8< ------------------------------------------------
        private static const Set<string> STATIC_SET;
        static construct {
                STATIC_SET = new HashSet<string> ();
                STATIC_SET.add ("Hello");
                STATIC_SET.add ("World");
        }
------------------------------------------------ >8

with this result:
8< ------------------------------------------------
valac --pkg gee-1.0 hello_set.vala
hello_set.vala:5.4-5.47: error: `Gee.Set<string>' not supported as type
for constants
        private static const Set<string> STATIC_SET;
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
hello_set.vala:8.3-8.16: error: The name `add' does not exist in the
context of `HelloTest.STATIC_SET'
                STATIC_SET.add("Hello");
                ^^^^^^^^^^^^^^
hello_set.vala:9.3-9.16: error: The name `add' does not exist in the
context of `HelloTest.STATIC_SET'
                STATIC_SET.add("World");
                ^^^^^^^^^^^^^^
Compilation failed: 3 error(s), 0 warning(s)
------------------------------------------------ >8


To my untrained eye this appears to indicate that I cannot use these as
constants.  If I drop 'const' from the declaration it compiles and runs
correctly.

Any suggestions?

Thank you.

-- 
Regards,
Brian




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