I have been trying to write clearer fortran code by using enums but it seems using a type variable instead would be better in terms of code readability.
you can find the code here: https://github.com/AAGAN/fortran_type_clean_code/blob/main/rang.f95
Program Rang
use iso_fortran_env, only : int8, int16, int32, int64
implicit none
! mix and match integer types for demonstration
integer(int16) :: red
integer(int32) :: blue
integer(int64) :: col
! create a type with some integer values
!(probably it's better to put this in a module)
type colortype
integer(int8) :: red = 1, blue = 2, veryDarkGreen = 3
end type colortype
! instantiate a variable of type colortype to use in the rest of the program
type(colortype) :: color
! output: 1 2 3
print *, color%red, color%blue, color%veryDarkGreen
red = color%red
if (red .eq. color%red) then
print *, "it is red"
end if
blue = 2
if (blue .eq. color%blue) then
print *, "it is blue"
end if
col = color%veryDarkGreen
if (col .eq. color%blue) then
print *, "it is not blue"
else if (col .eq. color%veryDarkGreen) then
print *, "it is very dark green"
else
print *, "it is red"
end if
End Program Rang
..
True, enums seldom if ever improve code readability or logic.
I have been trying to write clearer fortran code by using enums but it seems using a type variable instead would be better in terms of code readability.
you can find the code here: https://github.com/AAGAN/fortran_type_clean_code/blob/main/rang.f95 ..
On Wednesday, December 8, 2021 at 12:47:39 PM UTC-5, Gary Scott wrote:interoperability with C but it provides no type safety in actual usage and it doesn't really add much that named constants (PARAMETER attribute) does not provide already.
..
True, enums seldom if ever improve code readability or logic.
Well, Fortran does *not* have any meaningful facility with ENUMs. So *no* such assertion can be made in the context of Fortran. Fortran includes some rudimentary support toward ENUMs that was introduced starting Fortran 2003 along with
With other languages though that do include well-feature ENUMs feature, ENUMs do indeed help greatly while coding and readability and maintenance and also with usage such as with libraries.
program demo_enum
! pseudo-enum
type enum_colors
integer :: RED
integer :: BLUE
integer :: GREEN
end type enum_colors
type(enum_colors),parameter :: colors=enum_colors(1,2,3)
integer :: col
col=colors%red
select case(col)
case(colors%red)
write(*,*)'red'
case(colors%green)
write(*,*)'green'
case(colors%blue)
write(*,*)'blue'
end select
end program demo_enum
(test1) urbanjs@venus:~$ vi /tmp/tmp.f90
(test1) urbanjs@venus:~$ nvfortran /tmp/tmp.f90
(test1) urbanjs@venus:~$ ./a.out
red
(test1) urbanjs@venus:~$ ifort /tmp/tmp.f90
(test1) urbanjs@venus:~$ ./a.out
red
(test1) urbanjs@venus:~$ gfortran /tmp/tmp.f90
(test1) urbanjs@venus:~$ ./a.out
red
Worked with at least these three
program test_universal_constants
use M_constants, only : uc
write(*,*)uc%qp%gamma ! universal constant, quad-precision, gamma >associate (gamma => uc%dp%gamma)
write(*,*)gamma
end associate
end program test_universal_constants
module M_constants
use, intrinsic :: iso_fortran_env, only : r4=>real32, r8=>real64, r16=>real128 >implicit none
private
real(r16),parameter :: &
pi = 3.141592653589793238462643383279502884197169399375105820974944592307_r16, &
gamma = 0.577215664901532860606512090082402431042_r16, &
e = 2.71828182845904523536028747135266249775724709369995_r16, &
Golden_Ratio = 1.6180339887498948482045868_r16, &
euler = 0.577215664901532860606512090082402431042_r16, &
Deg_Per_Rad = 57.2957795130823208767981548_r16, &
Rad_Per_Deg = 0.01745329251994329576923691_r16
type r128; real(r16) :: pi,gamma,e,golden_ratio,euler; endtype r128
type r64; real(r8) :: pi,gamma,e,golden_ratio,euler; endtype r64
type r32; real(r4) :: pi,gamma,e,golden_ratio,euler; endtype r32
type rall
type(r128) :: qp
type(r64) :: dp
type(r32) :: sp
end type rall
type(rall),parameter,public :: uc=rall( &
& r128(pi=pi,gamma=gamma,e=e,golden_ratio=golden_ratio,euler=euler), &
& r64(pi=real(pi,r8),gamma=real(gamma,r8),e=real(e,r8),golden_ratio=real(golden_ratio,r8),euler=real(euler,r8) ), &
& r32(pi=real(pi,r4),gamma=real(gamma,r4),e=real(e,r4),golden_ratio=real(golden_ratio,r4),euler=real(euler,r4) ) &
& )
end module M_constants
real,parameter :: e=uc%sp%e
unfortunately values declared in a user-defined type cannot have the PARAMETER attribute, which might make it a bit more elegant.
unfortunately values declared in a user-defined type cannot have the PARAMETER attribute, which might make it a bit more elegant.
John’s last syntax example will not work for this,...
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 159 |
Nodes: | 16 (0 / 16) |
Uptime: | 98:25:39 |
Calls: | 3,209 |
Files: | 10,563 |
Messages: | 3,009,579 |