Dennis Möhlmann | 21 Aug 2012 05:34

[Vala] foreach / Gee.Tree/HashMap

Hello,

I was running into an issue with Gee Maps and was wondering if this is
intended behaviour. Here's a minimal sample:

var map = new HashMap<int, string>();
map[1] = "first";
map[2] = "second";

foreach (var value in map) {
    stdout.printf((value ?? "null") + "\n");
}

stdout.printf("--\n");

foreach (var entry in map.entries) {
    stdout.printf((entry.value ?? "null") + "\n");
}

The resulting output is:

null
first
--
first
second

The first loop seems to start at the non-existing key 0. Adding map[0] =
"test" seems to confirm that:

(Continue reading)

Luca Bruno | 21 Aug 2012 08:41
Picon
Gravatar

Re: [Vala] foreach / Gee.Tree/HashMap

On Tue, Aug 21, 2012 at 5:34 AM, Dennis Möhlmann <in.dev@...> wrote:

> Hello,
>
> I was running into an issue with Gee Maps and was wondering if this is
> intended behaviour. Here's a minimal sample:
>
> var map = new HashMap<int, string>();
> map[1] = "first";
> map[2] = "second";
>
> foreach (var value in map) {
>     stdout.printf((value ?? "null") + "\n");
> }
>

> [...]
>

> "Gaps" in the map-keys produce similar issues. Looks like a bug to me,
> but I thought I'd ask here first.
>
>
That's because HashMap has no iterator() method, then vala uses the
size()/get() iterator mode. It's intended to work like that but absolutely
counter intuitive.

--

-- 
www.debian.org - The Universal Operating System
(Continue reading)

Dennis Möhlmann | 21 Aug 2012 11:47

Re: [Vala] foreach / Gee.Tree/HashMap

Hello,

thank you for the explanation. Pretty bad pitfall though.

Regards,
  Dennis

Am 21.08.2012 08:41, schrieb Luca Bruno:
> On Tue, Aug 21, 2012 at 5:34 AM, Dennis Möhlmann <in.dev <at> nyda.info
> <mailto:in.dev <at> nyda.info>> wrote:
> 
>     Hello,
> 
>     I was running into an issue with Gee Maps and was wondering if this is
>     intended behaviour. Here's a minimal sample:
> 
>     var map = new HashMap<int, string>();
>     map[1] = "first";
>     map[2] = "second";
> 
>     foreach (var value in map) {
>         stdout.printf((value ?? "null") + "\n");
>     }
> 
>  
> 
>     [...]
> 
>  
> 
(Continue reading)


Gmane