On Tue, 2012-10-09 at 21:35 +0200, Stefan Mauerberger wrote:
> Hello Bill, Hallo Neil, Hallo Van,
>
> thank you very much for your answers.
>
> As Van pointed out, someone needs a type-gather to make the returned
> value of polymorphic type explicit. This is exactly what I'd like to
> circumvent.
> I'd rather have a function whose result is ether of intrinsic type real
> or integer, depending on a single passed argument of type integer.
> (sorry for being spoiled by python) Could you think of any way achieving
> such functionality with Fortran?
Your function correctly does what you want. Fortran has limited
semantics for run-time polymorphism. Without explicit syntax,
polymorphic objects can only be allocated, deallocated, assigned, passed
as arguments, used to dispatch type-bound procedures, and their types
can be inquired. Otherwise, you need to add explicit syntax, i.e., the
SELECT TYPE construct.
What does python do that you think Fortran ought to do? If Fortran
already does it, but you like the way python does it better, how should
Fortran do it?
The function could be a bit more tersely written if Fortran had a
polymorphic if-then-else operator, such as the C ?: operator (spelt
differently if you don't like that syntax). If would have a polymorphic
result if the expressions surrounding : don't have the same declared
type and kind. If they are type and kind compatible, the declared type
would be that of the one that is not an extension. Otherwise, it would
be CLASS(*). Your function would then have one executable statement
instead of a SELECT CASE construct:
allocate ( value, source = key==1 ? 1 : 2.0 ) ! source is CLASS(*)
or
allocate ( value, source = if ( key==1 ) then 1 else 2.0 endif )
which wouldn't work in fixed form:
allocate(value,source=if(key==1)thenoneelsetwoendif)
Fortran doesn't have variables or expressions whose values are types,
but if it did, one might write
allocate ( key==1 ? integer : real :: value )
> Thank and all the best,
> Stefan
>
>
> Btw.: As being the only one asking beginner questions, is this a
> appropriate mailing-list bothering you guys with such low-lever stuff?
>
>
>
>
> On Fri, 2012-10-05 at 11:35 -0700, 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".
> >
> > > Cheers and thanks in advance,
> > > Stefan
> >
|