Print

Print


On 10/5/12 1:35 PM, Van Snyder wrote:
> On Fri, 2012-10-05 at 13:00 +0200, Stefan Mauerberger wrote:
>> Hi There,
>>
>> its me again. This time I stuck with Fortran's polymorphism. What I try
>> to implement is a function whose return value type is not fixed at
>> compile time. In other words, it should return ether a real or integer
>> which is already explicit.
>>
>> Just a toy example which, unfortunately, does not what I want:
>>
>> function get_value(key) result( value )
>>      integer, intent(in) :: key
>>      class(*), allocatable :: value
>>
>>      select case(key)
>>          case(1) ! should return integer
>>              allocate( value, source = 1 )
>>              return
>>          case(2) ! should return real
>>              allocate( value, source = 2.0 )
>>              return
>>      end select
>> end function get_value
>>
>> My issue is, the function's result is of implicit polymorphic type.
>> However, I want the result being explicit of intrinsic type integer or
>> real.
>>
>> Could you think of any solution archiving such functionality?
>
> This works with NAG 5.2(668):
>
> program Mauerberger
>
>    integer :: I
>    class(*), allocatable :: value
>    do i = 1, 2
> !    value = get_value(i)
>      select type ( value => get_value(i) )
>      type is ( integer )
>        print *, 'Value is integer = ', value
>      type is ( real )
>        print *, 'Value is real = ', value
>      end select
>    end do
>
> contains
>
>    function get_value(key) result( value )
>        integer, intent(in) :: key
>        class(*), allocatable :: value
>
>        select case(key)
>            case(1) ! should return integer
>                allocate( value, source = 1 )
>                return
>            case(2) ! should return real
>                allocate( value, source = 2.0 )
>                return
>        end select
>    end function get_value
>
> end program Mauerberger
>
> Both NAG 5.2(668) and ifort 12.0 Build 20101116 incorrectly objected to
> the assignment to the local allocatable variable "value".

I think Intel 12.0 requires a command line option to enable the 
auto-allocate-on-assignment  capability.

Cheers,
Bill



>
>> Cheers and thanks in advance,
>> Stefan

-- 
Bill Long                                           [log in to unmask]
Fortran Technical Support    &                 voice: 651-605-9024
Bioinformatics Software Development            fax:   651-605-9142
Cray Inc./Cray Plaza, Suite 210/380 Jackson St./St. Paul, MN 55101