Is it possible to detect the type of a variable?

Great question and post, it is something the committee has pondered over a lot and Fortran 202X introduces support via 2 new intrinsics, TYPEOF and CLASSOF c.f. https://wg5-fortran.org/N2151-N2200/N2194.pdf

2.3 US 16. The specifiers typeof and classof

The specifier typeof is available to declare one or more entities to be nonpolymorphic
with the type and type parameters of a previously declared entity. The previous entity
may have intrinsic type. It may be polymorphic, in which case its declared type is used; 
it must not be unlimited polymorphic or abstract.

The specifier classof is available to declare one or more entities to be polymorphic with 
the declared type and type parameters of a previously declared entity. The previous 
entity must not be of assumed type or intrinsic type. It may be unlimited polymorphic.

For both typeof and classof, if the previous entity is an optional dummy argument it 
must not have a deferred or assumed type parameter, but a type parameter is deferred
if it is deferred for the previous entity and this is not an optional dummy argument.

But my understanding is the above only addresses the declaration aspect, that there will remain limitations when it comes to referencing the objects; cascaded SELECT TYPE will still be needed to "dereference* an unlimited polymorphic dummy argument (a la a pointer) to a given type.

That the proper solution here will be the Generics capability to be introduced (hopefully) starting Fortran 202Y.

But I may be wrong on this.

1 Like