fortran90 - Have a function in fortran return a reference that can be placed on the left-hand-side of an assignment -
as stated in title, want directly modify data access through pointer retrieved function. having reference returned function appearing on l.h.s. of assignment(=) no issue in c++ following minimal example in fortran errors out:
module test_mod implicit none integer, target :: a=1, b=2, c=3 ! member variables contains function get(i) integer, pointer :: integer, intent(in) :: select case (i) case (1) => case (2) => b case (3) => c end select end function end module test_mod program test use test_mod implicit none integer, pointer :: i_p !> prints out 1 2 3 print*, get(1), get(2), get(3) !> want error !> error: 'get' @ (1) not variable get(2) = 5 !> works not want i_p => get(2) i_p = 5 end program test
is there way accomplish behaviour; maybe i'm missing attributes? bypass writing setter routines such as
set(i,value)
since should mimic appearance of array. in application, member variables a,b,c
arrays of different size
a = [a1, a2, a3] b = [b1, b2] c = [c1]
and want getter get(i,j)
mimic matrix of pointers
j = 1 2 3 = 1: [[a1, a2, a3], = 2: [b1, b2, xx], = 3: [c1, xx, xx]]
wehre xx
referencing null()
.
update: using gfortran (version 5.2.0) , deployment machines have versions starting 4.6.x , upwards. therefore, suggested fortran 2008 standard features unfortunately not available me. possible mimic behaviour described above without having compiler supporting out of box?
update 2: ended implementing structure follows
type vec_t integer, allocatable, dimension(:) :: vec end type vec_t type(vec_t), allocatable, dimension(:), target :: data
which initialise (my triangular matrix application mention @ end)
allocate(data(max)) i=1,max allocate(data(i)%vec(i)) end
and access & write through
print*, data(2)%vec(1) data(2)%vec(1) = 5
which not precisely after enough application.
let's @ want do:
get(2)=5
and error message
error: 'get' @ (1) not variable
that looks pretty comprehensive: can't want. or, perhaps...
get(2)
indeed, under rules of fortran 2003, not variable. in fortran 2003 variable given rules r601 , r603, list of designators.
the left-hand side of assignment must variable.
but @ fortran 2008 , definition of variable. variable either 1 of same designators (or ones related coarrays or complex parts), (c602 r602) function reference which
shall have data pointer result.
this summarized in introduction of fortran 2008, detailing extensions on fortran 2003, as
a pointer function reference can denote variable in variable definition context.
get(2)
reference function has data pointer result. get(2)
may appear on left-hand side of assignment statement under rules of fortran 2008.
alas, interpretation of fortran not supported current compilers: @ time of answering cray compiler.
this means answer saying have 2 options: switch compiler or wait until feature more widespread. both of these impractical, want answer gives more portable workaround.
i prefer link that given innospg, although latter based on former, description of appropriate field "pointer functions - pointer function ref variable" more clear. is, though, more accessible document , viable alternative.
Comments
Post a Comment