Skip to content

Simple Functions in Constant Expressions #253

@everythingfunctional

Description

@everythingfunctional

The 202X standard will include a new attribute for procedures, simple. If a procedure is simple it depends on, and uses only its arguments, and calls only simple procedures. This means that, in theory, if a simple function's arguments are themselves constant expressions, it could be evaluated before program startup. I contend that this would be quite valuable for defining named constants of derived types whose components are private. For example given a string type defined like

module string_m
  implicit none
  private
  public :: string_t
  type :: string_t
    private
    character(len=:), allocatable :: str_
  end type

  interface string_t
    module procedure constructor
  end interface
contains
  simple function constructor(str) result(string)
    character(len=*), intent(in) :: str
    type(string_t) :: string

    string%str_ = str
  end function
end module

It should be possible to define something like the following

type(string_t), parameter :: lines(*) =  &
    [ string_t("Hello, World!") &
    , string_t("It's a great day!") &
    , string_t("Hooray Fortran!") &
    ]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Clause 10Standard Clause 10: Expressions and assignment

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions