Newbie questions about Pliant
clearing an array
array to be returned is not cleared. |
Message posted by maybe Boris Reitman on 2007/01/20 23:32:01 |
The function below clears item_list array properly only if I uncomment item_list size := 0. Otherwise, the caller of this function gets a return value that is polluted from a previous call to the function.
function catalog_search query -> item_list arg Str query arg Array:Str item_list item_list := (var Array:Str empty) #item_list size := 0 var List:Str tokens := lower:query hungry_split " " each record flower_database:data if (match_item_codes query keyof:record) console "HERE" eol item_list += keyof:record else var FlowernetItem fnitem fnitem init "ftd" keyof:record var Str f_title := lower fnitem:get_title each token tokens if token <> "" and (f_title search token -1) <> -1 console "THERE: " token eol item_list += keyof:record
The caller code:
function test var Array:Str items := catalog_search "orchid" ok items:size > 1 is get_title:(items:0) "Lavender Phalaenopsis Orchid" for (var Int count) 0 items:size-1 console "Got " items:count eol var Str code := items:0 var Array:Str items2 := catalog_search code is items2:size 1 is get_title:(items2:0) "Lavender Phalaenopsis Orchid" for (var Int count) 0 items2:size-1 console "Got' " items2:count eol
The output: 1..25 THERE: orchid THERE: orchid THERE: orchid THERE: orchid THERE: orchid THERE: orchid THERE: orchid THERE: orchid THERE: orchid THERE: orchid THERE: orchid THERE: orchid THERE: orchid ok ok Got C34-3579 Got Intercat-1711 Got Intercat-2001 Got Intercat-2225 Got Intercat-2408 Got Intercat-2604 Got Intercat-3006 Got Intercat-3407 Got Intercat-3716 Got Intercat-4848 Got W10-3354 Got W8-3862 Got WG916 HERE not ok #expected 1 but got 14 ok Got' C34-3579 Got' Intercat-1711 Got' Intercat-2001 Got' Intercat-2225 Got' Intercat-2408 Got' Intercat-2604 Got' Intercat-3006 Got' Intercat-3407 Got' Intercat-3716 Got' Intercat-4848 Got' W10-3354 Got' W8-3862 Got' WG916 Got' C34-3579
If I uncomment the line: item_list size := 0, then this is the output:
1..25 THERE: orchid THERE: orchid THERE: orchid THERE: orchid THERE: orchid THERE: orchid THERE: orchid THERE: orchid THERE: orchid THERE: orchid THERE: orchid THERE: orchid THERE: orchid ok ok Got C34-3579 Got Intercat-1711 Got Intercat-2001 Got Intercat-2225 Got Intercat-2408 Got Intercat-2604 Got Intercat-3006 Got Intercat-3407 Got Intercat-3716 Got Intercat-4848 Got W10-3354 Got W8-3862 Got WG916 HERE ok ok Got' C34-3579
As you see, in the correct version I have only one "Got'" line. It seems that the line "item_list size := 0" triggers the initialization mechanism, because even if I just query the value of "item_list:size" like this, console items_list:size eol it works too.
Thanks, Boris |
Message posted by maybe Hubert Tonneau on 2007/01/21 02:11:38 |
This is a bug with the Pliant code generator optimiser.
One of the features of the Pliant code optimiser is to try to avoid copying as much as possible through sharing the variables.
As an example, if you have: d := a+b+c the compiler produces: t1 := a+b t2 := t1+c d := t2 where 't1' and 't2' are temporary variables, and the code optimiser tries to share 't2' with 'd' in order to save one copy.
So, when you write item_list := var Array:Str empty Pliant code generator just incorrectly assumed that it could share 'item_list' and 'empty'
When you write item_list size := 0 no wrong optimisation is possible, and just adding a 'console' in your function may also prevent the wrong optimisation because one of the variable will be used at a new place adding some contrains that can prevent the optimisation to be wrongly concluded as possible.
The bad news is that correcting such a bug can take a very long time because fixing a code generator optimiser is a nightmare. |