(j3.2006) Dynamic type of parent of dynamic type of an object

Tom Clune Thomas.L.Clune
Wed Mar 2 16:17:27 EST 2016


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













-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.j3-fortran.org/pipermail/j3/attachments/20160302/0eca908d/attachment-0003.html 



More information about the J3 mailing list