(j3.2006) a question on same_type_as and type parameters

Van Snyder Van.Snyder
Thu Aug 28 14:35:18 EDT 2008


Jim's example illustrates one of the reasons I wanted a NEWTYPE thing-o
instead of TYPEALIAS.  typedef in C is a bad idea that we were well
advised not to perpetuate in Fortran, but NEWTYPE has legitimate uses.

Suppose, for example, that I have given separate kind type parameter
values to different categories of REAL variables in my program.
Everything compiles and runs and gets the correct answer.  Then I change
one of them, and it stops working, because I didn't specify some dummy
argument somewhere using the correct named constant -- rather, a
different one that happened to have the same value as the correct one.

With a type calculus, I could have defined a new type, and the compiler
would have caught this maintenance problem in the first place.

On Thu, 2008-08-28 at 10:09 -0400, Jim Xia wrote:
> 
> Just to clarify this issue.  Consider the following program 
> 
>     class(*), allocatable :: a, b 
>     
>     allocate (real :: a) 
>     allocate (double precision :: b) 
>     
>     print *, same_type_as (a, b) 
>     end 

With a type calculus, one could define something like

  newtype :: REAL_S => REAL, REAL_D => DOUBLE PRECISION
  class(*), allocatable :: A, B
  allocate ( real_s :: a )
  allocate ( real_d :: b )

  print *, same_type_as ( a, b )

would print F (if same_type_as were allowed for unlimited polymorphic).

Indeed, if the types had been defined

  newtype :: REAL_S => REAL, REAL_D => REAL

it would still print F because every name defined by NEWTYPE would be
the name of a new type (hence the keyword choice).

It is possible for one to discover that the kinds of A and B in Jim's
example are different, by using SELECT TYPE constructs, but it's an ugly
mess.

> Should the PRINT statement print true or false?  Based on the
> standard, it seems that only type matters in deterring the value of
> the intrinsic, so it appears the program should print true.  However
> this may not provide enough information for the user since there is no
> way to query a or b's type parameter values.  So it's still unclear to
> the user, for example, if the two entities are of the same size so
> it's safe to do an internal memory transfer from a to b.  So the
> question is: do we really intend to have the SAME_TYPE_AS only checks
> for the types of two variables and ignore their type parameter
> values? 
> 
> The same question also applies to parameterized derived types as can
> be illustrated by the following example 
> 
> 
>     type t (k) 
>         integer, kind :: k 
> 
>         real :: x(k) 
>     end type 
>     
>     type (t(5)) a 
>     type (t(10)) b 
>     
>     print *, same_type_as (a, b) 
>     end 
> 
> 
> Jim Xia
> 
> RL Fortran Compiler Test
> IBM Toronto Lab at 8200 Warden Ave, Markham, On, L6G 1C7
> Phone (905) 413-3444  Tie-line 313-3444
> email: jimxia at ca.ibm.com
> D2/YF7/8200 /MKM
> _______________________________________________
> J3 mailing list
> J3 at j3-fortran.org
> http://j3-fortran.org/mailman/listinfo/j3




More information about the J3 mailing list