M_anything(3fm) - [M_anything::INTRO] procedures that use polymorphism to allow arguments of different types generically (LICENSE:MIT)
Synopsis
Description
Examples
Author
License
Syntax:
use M_anything,only : anyscalar_to_string use M_anything,only : anyscalar_to_int64 use M_anything,only : anyscalar_to_real use M_anything,only : anyscalar_to_real128 use M_anything,only : anyscalar_to_double use M_anything,only : anything_to_bytes use M_anything,only : anyinteger_to_string use M_anything,only : get_type use M_anything,only : bytes_to_anything use M_anything,only : empty, assignment(=)
anyscalar_to_string convert intrinsic type to string anyscalar_to_int64 convert integer or real of any kind to 64-bit integer anyscalar_to_real convert integer or real of any kind to real anyscalar_to_real128 convert integer or real of any kind to real128 anyscalar_to_double convert integer or real of any kind to doubleprecision anything_to_bytes convert anything to bytes anyinteger_to_string convert integer to string get_type return array of strings containing type names of arguments empty create an empty array
At the cost of casting to a different type these functions can (among other uses such as in linked lists) allow for an alternative to duplicating code using generic procedure methods. For example, the following SQUAREALL function can take many input types and return a DOUBLEPRECISION value (it is a trivial example for demonstration purposes, and does not check for overflow, etc.).:
Sample programprogram demo_M_anything use, intrinsic :: iso_fortran_env, only : int8, int16, int32, int64 use, intrinsic :: iso_fortran_env, only : real32, real64, real128 implicit none ! call same function with many scalar input types write(*,*)squareall(2_int8) write(*,*)squareall(2_int16) write(*,*)squareall(2_int32) write(*,*)squareall(2_int64) write(*,*)squareall(2.0_real32) write(*,*)squareall(2.0_real64) write(*,*)squareall(2.0_real128) contains
function squareall(invalue) result (dvalue) use M_anything, only : anyscalar_to_double class(*),intent(in) :: invalue doubleprecision :: invalue_local doubleprecision :: dvalue invalue_local=anyscalar_to_double(invalue) dvalue=invalue_local*invalue_local end function squareall
end program demo_M_anything
Results:
4.00000000000000 4.00000000000000 4.00000000000000 4.00000000000000 4.00000000000000 4.00000000000000 4.00000000000000
John S. Urban
Nemo Release 3.1 | M_anything (3) | February 23, 2025 |