On Mar 24, 2006, at 18:38, Chip Salzenberg wrote:
On Fri, Mar 24, 2006 at 04:23:53PM +0100, Leopold Toetsch wrote:
There are currently 14 opcode variants of C<new>, not counting
C<newclass>. But all are limited when it comes to initializers.
What's the situation in which instantiate is required because the
current
arrangment doesn't work? Generally, it's good to know what problem
you are
trying to solve before you try to solve it. :-)
Well, maybe the intro was too sort ;) But it clearly states, that the
14 C<new> opcodes aren't capable of creating arbitrary class instances
(not even all kinds of PMCs). Folks are creating custom object.build
methods, which after __init was called, initialize the object more.
Another way is to use the C<new_p_ic_p> opcode, which takes some kind
of initializer and pass a Hash to C<init_pmc>. Or the rest of init is
just done with more methods.
E.g. from SYNOPSIS of runtime/parrot/library/SDL/Button.pir
# the image to use for the button
$P0 = new PerlString
$P0 = "filename/to/image.png"
# create the button
$I0 = find_type 'SDL::Button'
button = new $I0, $P0
# set the position
button.'xpos'( 10 )
button.'ypos'( 10 )
The first init arg (the image name) is passed directly, then there are
some further method calls that set further object attributes.
This works of course, but is just a workaround of the HLL's usage of
some 'o = Klas(args)' syntax. And it's not standardized and not
exchangable between compilers.
Both variants are almost equally fast[2], but the latter has much more
potential of passing args e.g.
o = __instantiate(.Complex, 2, 3) # [3]
o = __instantiate(.Complex, "2 + 3i")
or
cl = getclass 'MyClass'
o = __instantiate(cl, p0, p1, 'extra' => p2)
Why not use the existing method call technique for this, and simply
standardize on a method name?
A method call requires a PMC. Above scheme (a function call with
standardized name) also works with class integer IDs (and class name
strings, when implemented). It saves plenty of time on the class
lookup, when called with integer IDs.
The C<set_args> / C<instantiate> opcodes could of course be united to a
more general C<new> opcode, that takes a variable amount of
initializers. And one standardized scheme of object creation is of
course much faster than all the current workarounds.
leo