bmx-ng / bcc

A next-generation bcc parser for BlitzMax
zlib License
33 stars 12 forks source link

Generics self increment issue #614

Open thareh opened 1 year ago

thareh commented 1 year ago

Hey,

Seems like "self increment" i.e. var :+ 1 for instance isn't working properly with generics.

Switch the commented line with the one beneath it and you'll see the issue at hand.

Framework BRL.Blitz
Import BRL.Collections
Import BRL.StandardIO
Import Random.Core
Import BRL.Random

Local map:TTreeMap<Int, Int> = New TTreeMap<Int, Int>()

For Local i:Int = 0 Until 1000
    Local key:Int = Rand(0, 10)

'   map[key] :+  1
    map[key] = map[key] + 1
Next

For Local i:Int = EachIn map.Keys()
    Print i + ": " + map[i]
Next

Thanks and have a good day!

GWRon commented 1 year ago

The "map = map + 1" generates:

(bbt_map)->clas->m__iset_ii((struct _m_untitled1_TTreeMapii_obj*)bbt_map,bbt_key,((bbt_map)->clas->m__iget_i((struct _m_untitled1_TTreeMapii_obj*)bbt_map,bbt_key)+1));

while "map :+ 1" results in:

(bbt_map)->clas->m__iset_ii((struct _m_untitled1_TTreeMapii_obj*)bbt_map,bbt_key,1);

btw "map = 1" also results in:

(bbt_map)->clas->m__iset_ii((struct _m_untitled1_TTreeMapii_obj*)bbt_map,bbt_key,1);
woollybah commented 9 months ago

map[key] returns the value from the map. It is not a reference to the value within the map. If you want to change the value within the map, you can map[key] = map[key] + 1

GWRon commented 9 months ago

I think the issue is that :+ is a shortcut to x = x +1 while the above code seems to make it x = 1