(j3.2006) Dynamic type of parent of dynamic type of an object
Van Snyder
Van.Snyder
Wed Mar 2 18:41:31 EST 2016
I had in mind some kind of addition to the SELECT TYPE construct that
wouldn't need the parent type name. Something like
select type ( X )
ancestor ( N )
! X is polymorphic, but its dynamic type is the dynamic type of the
! n'th ancestor of its dynamic type.
blah blah blah
end select
The "N" in the "ancestor" statement should be a constant and constrained
to be greater than zero; the upper bound of N can't be constrained, but
a runtime requirement could be "the dynamic type of the selector shall
not have fewer than N ancestors." Or maybe it's not necessary for it to
be a constant.
A belt-and-suspenders program could verify the correct height of the
dynamic type with a new intrinsic function NUMBER_OF_ANCESTORS.
Others have asked for a list in the CLASS IS statement, with the
associate name being polymorphic and having a dynamic type that is
whichever of the listed types that is the dynamic type of X or an
ancestor of its dynamic type. Although a bit wordier, one could then
write
select type ( X )
class is ( A2, B2 )
! Here we know the dynamic type of X has enough ancestor types
select type ( X )
ancestor ( N )
blah blah blah
end select
class default
stop "I don't know what to do here"
end select
On Wed Mar 2 14:17:27 MST 2016, Thomas L Clune wrote:
> I don?t think you would want to do this automagically. The methods
> _were_ overridden for A2 and B2. You _should_ have to do something
> extraordinary to thwart that intent.
>
> Having said that, a solution that avoids the sin of SELECT TYPE would be to have a separate method (say ?cheap()?) in the base class(es) that has the less expensive implementation. f() can then be implemented by invoking cheap() in A1 and B1. A2 and B2 would then override f with expensive implementations. A user that wants the cheap method would then use the cheap() method directly.
>
> Other solutions also present themselves, but all have about the same number of wrinkles I would guess.
>
> - Tom
>
>
>
> > On Mar 2, 2016, at 3:12 PM, Van Snyder <Van.Snyder at jpl.nasa.gov> wrote:
> >
> > I was asked "How does one get the dynamic type of the parent type of the
> > dynamic type of a polymorphic object?"
> >
> > T is the base type.
> >
> > A1 extends T and A2 extends A1
> > B1 extends T and B2 extends B1
> >
> > X is polymorphic of declared type T and dynamic type either A2 or B2.
> >
> > It is desired to run a method bound to A1 or B1, that has been
> > overridden in A2 and B2. The overriding method is somewhat more
> > expensive than the overridden one, the overridden one produces the
> > desired result, the overriding one produces the desired result and also
> > some uninteresting stuff.
> >
> > It can be done with a SELECT TYPE construct, but then one has the type
> > hierarchy wired into the source text of the code. It would be nice to
> > do it automagically.
> >
> > select type ( x )
> > class is ( A2 )
> > y = x%a1%f(42)
> > class is ( B2 )
> > y = x%a1%f(42)
> > class default
> > stop "I don't know what to do here"
> > end select
> >
> >
> > _______________________________________________
> > J3 mailing list
> > J3 at mailman.j3-fortran.org
> > http://mailman.j3-fortran.org/mailman/listinfo/j3
>
> Thomas Clune, Ph. D. <Thomas.L.Clune at nasa.gov>
> Software Infrastructure Team Lead
> Global Modeling and Assimilation Office, Code 610.1
> NASA GSFC
> MS 610.1 B33-C128
> Greenbelt, MD 20771
> 301-286-4635
>
>
More information about the J3
mailing list