Re: Phix object arrays
- Posted by petelomax Feb 03, 2021
- 858 views
irv said...
/home/irv/pgtk/buttontest.ex:18 buttons[1].label = "One" ^ '=' expectedIt is often necessary to call a method for one or more of the items in the array, but as noted above, can't do that.
Is there another way?
The compiler needs a type hint, for example
Button b = buttons[1] b.label = "One"
or maybe (you could also use a generic struct b parameter here)
procedure set_label(Button b, string label) b.label = label end procedure set_label(buttons[1],"One")
Unfortunately it's just not smart enough to figure out that buttons[1] is a Button instance, not entirely dissimilar to
Button b = new() -- ok -- buttons[i] = new() -- not ok buttons[i] = new(Button) -- ok
In fact, I'll go so far as to recommend this version of your above for loop (one more line, but also one less subscript):
for i = 1 to length(buttons) do Button b = new() buttons[i] = b pan.add(b) end for