Hi all,

I'd like to register some subroutines defined in PASM (or better, PIR)
as participating in multiple dispatch.  This is very handy when writing
Test::Builder::is(), for example, which can compare two strings,
integers, numbers, or PMCs, or for calling NCI functions which can take
various types and numbers of arguments that all do very similar things.

Dan suggested a new op with a scheme similar to:

        register_mmd S1, S2, P

where S1 is the name of the multi sub that users will call, S2 is the
name of the sub to dispatch to, and P is an array of types that define
S2's signature.  The types are the integers as found in datatypes.pasm.

Here's some (rough) example PIR:

        .sub add_integers prototyped
                .param int x
                .param int y

                .local int sum
                sum = x + y

                .pcc_begin_return
                        .return sum
                .pcc_end_return
        .end

        .sub add_nums prototyped
                .param num x
                .param num y

                .local num sum
                sum = x + y

                .pcc_begin_return
                        .return sum
                .pcc_end_return
        .end

        .sub main
                .include 'datatypes.pasm'

                .local pmc signature
                signature    = new Array
                signature[0] = .DATATYPE_INT
                signature[1] = .DATATYPE_INT

                register_mmd 'add_op', 'add_ints', signature

                signature    = new Array
                signature[0] = .DATATYPE_NUM
                signature[1] = .DATATYPE_NUM

                register_mmd 'add_op', 'add_nums', signature

                .local int intsum
                .local num numsum

                intsum = add_op( 2, 3 )
                numsum = add_op( 2.5, 2.5 )

                print "Intsum: "
                print intsum
                print "\n"

                print "Numsum: "
                print numsum
                print "\n"
        .end
There are other options for declaring the signature.  There could be:

        register_mmd S1, S2, I1, I2, I3, P

where I1 is the number of arguments in the I register, I2 is the number
of arguments in the N register, I3 is the number of elements in the S
register, and P is an array of PMC types.

The first signature is pretty simple and the second is a complex
signature.  There may be something in between with a multi-level array
or more complex data structure.

There also may be some way of adding 'multi' to the end of a subroutine
declaration in PIR, if IMCC is capable of mucking around with symbols
based on the types of arguments the subroutine takes -- that'd be really
handy, but it's not necessary right now.

Comments welcome,
-- c

Reply via email to