Ticket #562 (new bug)

Opened 10 years ago

Last modified 10 years ago

subclassing basic number types fails to inherit basic ops like add, sub, mul and div

Reported by: bsdz Owned by: kjs
Priority: critical Milestone:
Component: core Version: trunk
Severity: high Keywords: pir subclass Integer Float Complex add mul div sub
Cc: Language: perl6
Patch status: Platform: all

Description

When subclassing base types like Integer, Float and Complex. The ops sub, add, mul and div are not properly inherited by the derived class.

A basic example is provided by the following code: -

.sub '' :main
    $P0 = subclass 'Integer', 'FooInt'
    
    print 'Integer: 10 - 1 = '
    $P0 = new 'Integer'
    $P0 = 10
    sub $P1, $P0, 1
    say $P1
 
    print 'FooInt: 10 - 1 = '
    $P0 = new 'FooInt'
    $P0 = 10
    #$P1 = new 'FooInt' # uncommenting here does not help
    sub $P1, $P0, 1
    say $P1 
.end

outputs the following: -

Integer: 10 - 1 = 9
FooInt: 10 - 1 = Multiple Dispatch: No suitable candidate found for 'subtract_int', with signature 'PIP->P'
...

This appears to be related to RT  http://rt.perl.org/rt3/Public/Bug/Display.html?id=59630 which outlines the problem for a specific case of a Complex type.

The attached script proves the bug also affects add, sub, mul, div for Integer, Float and Complex. It could be used as a test case.

Attachments

test2.pir Download (3.4 KB) - added by bsdz 10 years ago.
PIR demonstrating the general case.

Change History

Changed 10 years ago by bsdz

PIR demonstrating the general case.

Changed 10 years ago by mikehh

It looks like this ticket still applies.

I was investigating a problem with t/pmc/complex.t and extracted the following test:

#! parrot

.sub main :main
    .include 'test_more.pir'
    .include 'fp_equality.pasm'
    .include "iglobals.pasm"

    plan(3)

    $P0 = subclass 'Complex', 'MyComplex'
    addattribute $P0, "re"
    addattribute $P0, "im"

    .local pmc a, b, c
    ##   a = 1 + 2i
    a = new ['MyComplex']
    a['real'] = 1
    a['imag'] = 2
    is( a, "1+2i", 'a created' )

    ##   b = 3 + 4i
    b = new ['MyComplex']
    b['real'] = 3
    b['imag'] = 4
    is( b, "3+4i" , 'b created' )

    ##   a = a + b

    a = add a, b
    is( a, "4+6i", 'subclass add' )
.end

with the following results:

mhb@mhb-desktop:~/parrot$ date
Wed Dec  2 13:09:06 GMT 2009
mhb@mhb-desktop:~/parrot$ prove -v t/pmc/sub_comp.t
t/pmc/sub_comp.t ..
1..3
ok 1 - a created
ok 2 - b created
not ok 3 - subclass add
# Have: 0+0i
# Want: 4+6i
Failed 1/3 subtests

Test Summary Report
-------------------
t/pmc/sub_comp.t (Wstat: 0 Tests: 3 Failed: 1)
  Failed test:  3
Files=1, Tests=3,  0 wallclock secs ( 0.02 usr  0.00 sys +  0.01 cusr  0.02 csys =  0.05 CPU)
Result: FAIL

returning 0+0i and not doing the add but overwriting the original value.

Changed 10 years ago by pmichaud

  • lang set to perl6
  • component changed from pirc to core
Note: See TracTickets for help on using tickets.