2 ! Test the fix for PR42385, in which CLASS defined operators
3 ! compiled but were not correctly dynamically dispatched.
5 ! Contributed by Janus Weil <janus@gcc.gnu.org>
15 procedure :: times => times_foo
16 procedure :: assign => assign_foo
17 generic :: operator(*) => times
18 generic :: assignment(=) => assign
23 function times_foo(this,factor) result(product)
24 class(foo) ,intent(in) :: this
25 class(foo) ,allocatable :: product
26 integer, intent(in) :: factor
27 allocate (product, source = this)
28 product%foo_x = -product%foo_x * factor
31 subroutine assign_foo(lhs,rhs)
32 class(foo) ,intent(inout) :: lhs
33 class(foo) ,intent(in) :: rhs
34 lhs%foo_x = -rhs%foo_x
40 use foo_module ,only : foo
45 type ,extends(foo) :: bar
48 procedure :: times => times_bar
49 procedure :: assign => assign_bar
53 subroutine assign_bar(lhs,rhs)
54 class(bar) ,intent(inout) :: lhs
55 class(foo) ,intent(in) :: rhs
59 lhs%foo_x = -rhs%foo_x
62 function times_bar(this,factor) result(product)
63 class(bar) ,intent(in) :: this
64 integer, intent(in) :: factor
65 class(foo), allocatable :: product
68 allocate(product,source=this)
71 product%bar_x = 2*this%bar_x*factor
78 use foo_module ,only : foo
79 use bar_module ,only : bar
84 ! foo's assign negates, whilst its '*' negates and mutliplies.
86 call rescale(unitf, 42)
87 if (unitf%foo_x .ne. 42) call abort
89 ! bar's assign negates foo_x, whilst its '*' copies foo_x
90 ! and does a multiply by twice factor.
93 call rescale(unitb, 3)
94 if (unitb%bar_x .ne. 12) call abort
95 if (unitb%foo_x .ne. -1) call abort
97 subroutine rescale(this,scale)
98 class(foo) ,intent(inout) :: this
99 integer, intent(in) :: scale