Edit: I think what you deem inconsistent, isn’t allowed because of
C795 (R754) type-name shall be the name of an accessible derived type.
The name of the type declared within a function isn’t accessible in the context where the function is called. Accessibility implies that you either import the derived type from a module, or that it be available from the scope or via host association.
If you want to have a public type to create variables, you don’t need to nest it in the function body; you can just put it in the specification section of the parent scope (i.e. program, procedure, module.)
program picalc
implicit none
integer, parameter :: dp = kind(1.0d0)
type :: point
real(dp) :: x, y
end type
type(point) :: p ! okay here
! ...
contains
type(point) function random_point() result(p) ! and also here
! ...
end function
end program
@hratcliffe’s comment brings some extra light. It makes sense for creating external procedures callable from C without having to use modules:
! external (i.e. non-module) procedure
type(rgb_t) function color() bind(c)
type, bind(c) :: rgb_t
integer(c_int) :: r, g, b
end type
! ...
end function
You could stick the type definitions in an include file to remove repetition and guarantee consistency.
Same goes for sequence
types. I’ve only encountered one in practice. I imagine it is useful for loading/storing fixed-size objects from/to memory, say a bitmap file header, where it’s desirable the order of the structure is preserved exactly. I imagine another usage could be for a core dump or the register file of a (virtual) machine.
I do wonder with F2023 introducing the TYPEOF
declaration-type-spec, if this is allowed:
associate (f => foo())
block
typeof(f) :: g ! is this allowed?
! ...
end block
end associate
I’m not really sure what to say from the constraints in J3/24-007, section 7.3.2.1.
P.S. J3/24-007: It’s not just a standard, it’s a mission—bringing Fortran into the future with a license to compute.