A derived type can group related configuration fields before validation checks run.

Program

Play the program to change the worker count and validate the record.

workers
config_record_validation.f90
Replay: real traced execution (multi-file project)
program config_record_validation_demo
    implicit none
    type :: app_config
        integer :: port
        integer :: workers
        integer :: timeout
    end type app_config
    type(app_config) :: cfg
    integer :: valid_count

    cfg%port = 8080
    cfg%workers = 4
    cfg%timeout = 30
    valid_count = 0
    if (cfg%port > 0) valid_count = valid_count + 1
    if (cfg%workers > 0) valid_count = valid_count + 1
    if (cfg%timeout <= 60) valid_count = valid_count + 1
    print '(I0, 1X, I0)', cfg%workers, valid_count
end program config_record_validation_demo
program config_record_validation_demo
    implicit none
    type :: app_config
        integer :: port
        integer :: workers
        integer :: timeout
    end type app_config
    type(app_config) :: cfg
    integer :: valid_count

    cfg%port = 8080
    cfg%workers = 0
    cfg%timeout = 30
    valid_count = 0
    if (cfg%port > 0) valid_count = valid_count + 1
    if (cfg%workers > 0) valid_count = valid_count + 1
    if (cfg%timeout <= 60) valid_count = valid_count + 1
    print '(I0, 1X, I0)', cfg%workers, valid_count
end program config_record_validation_demo
program config_record_validation_demo
    implicit none
    type :: app_config
        integer :: port
        integer :: workers
        integer :: timeout
    end type app_config
    type(app_config) :: cfg
    integer :: valid_count

    cfg%port = 8080
    cfg%workers = 8
    cfg%timeout = 30
    valid_count = 0
    if (cfg%port > 0) valid_count = valid_count + 1
    if (cfg%workers > 0) valid_count = valid_count + 1
    if (cfg%timeout <= 60) valid_count = valid_count + 1
    print '(I0, 1X, I0)', cfg%workers, valid_count
end program config_record_validation_demo
  1. cfg%port ← 8080

    11cfg%port = 808012cfg%workers = 4
    values this step8080cfg%port
  2. cfg%workers ← 4

    11cfg%port = 808012cfg%workers = 413cfg%timeout = 30
    values this step4cfg%workers
  3. cfg%timeout ← 30

    12cfg%workers = 413cfg%timeout = 3014valid_count = 0
    values this step30cfg%timeout
  4. valid_count ← 0

    13cfg%timeout = 3014valid_count = 015if (cfg%port > 0) valid_count = valid_count + 1
    values this step0valid_count
  5. valid_count ← 1

    14valid_count = 015if (cfg%port > 0) valid_count = valid_count + 116if (cfg%workers > 0) valid_count = valid_count + 1
    values this step1valid_count.true.cfg%port > 0
  6. valid_count ← 2

    15if (cfg%port > 0) valid_count = valid_count + 116if (cfg%workers > 0) valid_count = valid_count + 117if (cfg%timeout <= 60) valid_count = valid_count + 1
    values this step2valid_count.true.cfg%workers > 0
  7. valid_count ← 3

    16if (cfg%workers > 0) valid_count = valid_count + 117if (cfg%timeout <= 60) valid_count = valid_count + 118print '(I0, 1X, I0)', cfg%workers, valid_count
    values this step3valid_count.true.cfg%timeout <= 60
  8. print '(I0, 1X, I0)', cfg%workers, valid_count

    17    if (cfg%timeout <= 60) valid_count = valid_count + 118    print '(I0, 1X, I0)', cfg%workers, valid_count19end program config_record_validation_demo
    output4 3
    values this step4cfg%workers3valid_count
  1. cfg%port ← 8080

    11cfg%port = 808012cfg%workers = 0
    values this step8080cfg%port
  2. cfg%workers ← 0

    11cfg%port = 808012cfg%workers = 013cfg%timeout = 30
    values this step0cfg%workers
  3. cfg%timeout ← 30

    12cfg%workers = 013cfg%timeout = 3014valid_count = 0
    values this step30cfg%timeout
  4. valid_count ← 0

    13cfg%timeout = 3014valid_count = 015if (cfg%port > 0) valid_count = valid_count + 1
    values this step0valid_count
  5. valid_count ← 1

    14valid_count = 015if (cfg%port > 0) valid_count = valid_count + 116if (cfg%workers > 0) valid_count = valid_count + 1
    values this step1valid_count.true.cfg%port > 0
  6. if (cfg%workers > 0) valid_count = valid_count + 1

    15if (cfg%port > 0) valid_count = valid_count + 116if (cfg%workers > 0) valid_count = valid_count + 117if (cfg%timeout <= 60) valid_count = valid_count + 1
    values this step.false.cfg%workers > 0
  7. valid_count ← 2

    16if (cfg%workers > 0) valid_count = valid_count + 117if (cfg%timeout <= 60) valid_count = valid_count + 118print '(I0, 1X, I0)', cfg%workers, valid_count
    values this step2valid_count.true.cfg%timeout <= 60
  8. print '(I0, 1X, I0)', cfg%workers, valid_count

    17    if (cfg%timeout <= 60) valid_count = valid_count + 118    print '(I0, 1X, I0)', cfg%workers, valid_count19end program config_record_validation_demo
    output0 2
    values this step0cfg%workers2valid_count
  1. cfg%port ← 8080

    11cfg%port = 808012cfg%workers = 8
    values this step8080cfg%port
  2. cfg%workers ← 8

    11cfg%port = 808012cfg%workers = 813cfg%timeout = 30
    values this step8cfg%workers
  3. cfg%timeout ← 30

    12cfg%workers = 813cfg%timeout = 3014valid_count = 0
    values this step30cfg%timeout
  4. valid_count ← 0

    13cfg%timeout = 3014valid_count = 015if (cfg%port > 0) valid_count = valid_count + 1
    values this step0valid_count
  5. valid_count ← 1

    14valid_count = 015if (cfg%port > 0) valid_count = valid_count + 116if (cfg%workers > 0) valid_count = valid_count + 1
    values this step1valid_count.true.cfg%port > 0
  6. valid_count ← 2

    15if (cfg%port > 0) valid_count = valid_count + 116if (cfg%workers > 0) valid_count = valid_count + 117if (cfg%timeout <= 60) valid_count = valid_count + 1
    values this step2valid_count.true.cfg%workers > 0
  7. valid_count ← 3

    16if (cfg%workers > 0) valid_count = valid_count + 117if (cfg%timeout <= 60) valid_count = valid_count + 118print '(I0, 1X, I0)', cfg%workers, valid_count
    values this step3valid_count.true.cfg%timeout <= 60
  8. print '(I0, 1X, I0)', cfg%workers, valid_count

    17    if (cfg%timeout <= 60) valid_count = valid_count + 118    print '(I0, 1X, I0)', cfg%workers, valid_count19end program config_record_validation_demo
    output8 3
    values this step8cfg%workers3valid_count

Follow the Checks

  1. The record starts with port=8080, workers=4, and timeout=30.
  2. valid_count starts at 0.
  3. port > 0 is true, so the count becomes 1.
  4. workers > 0 is true, so the count becomes 2.
  5. timeout <= 60 is true, so the count becomes 3 and the program prints 4 3. | workers | port check | workers check | timeout check | output | | ---: | --- | --- | --- | --- | | 0 | pass | fail | pass | 0 2 | | 4 | pass | pass | pass | 4 3 | | 8 | pass | pass | pass | 8 3 |
derived type `app_config` groups related configuration fields.
field access `cfg%workers` reads or writes one field on the record.
validation count `valid_count` summarizes how many checks passed.

Exercise: config_record_validation.f90

Reproduce 4 3, then use workers 0 and 8 to predict 0 2 and 8 3.