- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
I'm having trouble to define a overloaded assigment using:
- a scalar variable on the left-hande side and
- a 1d array on the right-hand side.
In other words, I want to do scalar = vector(:).
Is this even allowed ?
Thanks.
! ifort main.f90; ./a.out
Module MyModule
implicit none
private
public :: MyType
Type :: MyType
integer :: Var
contains
generic ,public :: operator(+) => Addition_0d, Addition_1d
procedure ,private :: Addition_0d
procedure ,private :: Addition_1d
generic ,private :: assignment(=) => Assign_0d, Assign_1d
procedure ,private :: Assign_0d
procedure ,private :: Assign_1d
End Type
contains
Function Addition_0d( This, That ) result(Res)
class(MyType) ,intent(in) :: This
type(MyType) ,intent(in) :: That
type(MyType) :: Res
Res%Var = This%Var + That%Var
End Function
Function Addition_1d( This, That ) result(Res)
class(MyType) ,intent(in) :: This
type(MyType) ,dimension(:) ,intent(in) :: That
type(MyType) :: Res
Res%Var = This%Var + sum( That%Var )
End Function
Subroutine Assign_0d( lhs, rhs )
class(MyType) ,intent(out) :: lhs
type(MyType) ,intent(in) :: rhs
lhs%Var = rhs%Var
End Subroutine
Subroutine Assign_1d( lhs, rhs )
class(MyType) ,intent(out) :: lhs
type(MyType) ,dimension(:) ,intent(in) :: rhs
lhs%Var = sum( rhs%Var )
End Subroutine
End Module
Program Main
use MyModule ,only: MyType
implicit none
type(MyType) :: Sca_1, Sca_2, Sca_3
type(MyType) ,dimension(3) :: Vec_1_2_3
Sca_1%Var = 1
Sca_2%Var = 2
Sca_3%Var = 0
write(*,"(/,'Test_1)')")
Sca_3 = Sca_1 + Sca_2
if ( Sca_3%Var == 3 ) then
write(*,"('Success: Sca_3%Var = ',g0)") Sca_3%Var
else
write(*,"('Failure: Sca_3%Var = ',g0)") Sca_3%Var
end if
Sca_3%Var = 0
Vec_1_2_3(1)%Var = 1
Vec_1_2_3(2)%Var = 2
Vec_1_2_3(3)%Var = 3
write(*,"(/,'Test_2)')")
Sca_3 = Sca_1 + Vec_1_2_3
if ( Sca_3%Var == 7 ) then
write(*,"('Success: Sca_3%Var = ',g0)") Sca_3%Var
else
write(*,"('Failure: Sca_3%Var = ',g0)") Sca_3%Var
end if
Sca_3%Var = 0
write(*,"(/,'Test_3')")
Sca_3 = Vec_1_2_3 ! COMPILER_ERROR: error #6366: The shapes of the array expressions do not conform. [SCA_3]
if ( Sca_3%Var == 6 ) then
write(*,"('Success: Sca_3%Var = ',g0)") Sca_3%Var
else
write(*,"('Failure: Sca_3%Var = ',g0)") Sca_3%Var
end if
End Program
Link Copied
2 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The assignment operator is marked private. You need to make it public otherwise it isn't usable outside the module. The first assignment works because it is assigning a scalar to a scalar and that's intrinsic.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Oooups... of course. I didn't double-checked the code.
Sorry for that.
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page