File: interface_31.f90

package info (click to toggle)
lfortran 0.61.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 61,892 kB
  • sloc: cpp: 181,767; f90: 92,175; python: 17,616; ansic: 10,170; yacc: 2,377; sh: 1,444; fortran: 892; makefile: 38; javascript: 15
file content (56 lines) | stat: -rw-r--r-- 1,452 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
! Test: abstract interface defined after the generic interface that uses it.
! Fortran allows forward references within a module specification section.
module interface_31_mod
    implicit none

    type :: result_t
        integer :: status = 0
    end type

    abstract interface
        subroutine callback_iface(x)
            integer, intent(in) :: x
        end subroutine
    end interface

    interface run
        module function run_impl(cmd, callback) result(res)
            character(*), intent(in) :: cmd
            procedure(callback_iface), optional :: callback
            type(result_t) :: res
        end function
    end interface
end module

submodule (interface_31_mod) interface_31_sub
contains
    module function run_impl(cmd, callback) result(res)
        character(*), intent(in) :: cmd
        procedure(callback_iface), optional :: callback
        type(result_t) :: res
        res%status = 1
        if (present(callback)) then
            call callback(42)
            res%status = 2
        end if
    end function
end submodule

program interface_31
    use interface_31_mod, only: result_t, run
    implicit none
    type(result_t) :: r

    r = run("test", callback=my_cb)
    if (r%status /= 2) error stop

    r = run("test2")
    if (r%status /= 1) error stop

    print *, "PASS"
contains
    subroutine my_cb(x)
        integer, intent(in) :: x
        if (x /= 42) error stop
    end subroutine
end program