Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I use interfaces?

Tags:

fortran

Whenever I program in Fortran I use modules and I don't have to worry about writing interfaces.

Now I'm writing Fortran code to use inside R. The problem is that the subroutines cannot be inside a module so I "have" to write interfaces. If I don't write the interface everything works fine, but smart internet people said I should write the interfaces.

Can someone explain me why? What are the benefit?

like image 732
Ignacio Avatar asked Jul 04 '26 20:07

Ignacio


1 Answers

As Alexander Vogt says, interface blocks can be useful for providing generic identifiers and allowing certain compiler checks.

If you're using interface blocks to create generic identifiers you're probably doing so within modules, so the main reason to write such blocks when modules aren't about is for the explicit interfaces that they create in the scope in which they occur. It's these that allow those compiler checks and the use association with modules no longer happens.

However, there are times when an explicit interface is required, rather than being a nicety: if referencing a procedure with certain features an explicit interface will be required for your code to conform to the Fortran standard. Those features can be found in F2008, 12.4.2.2

A procedure other than a statement function shall have an explicit interface if it is referenced and

  1. a reference to the procedure appears
    (a) with an argument keyword (12.5.2), or
    (b) in a context that requires it to be pure,
  2. the procedure has a dummy argument that
    (a) has the ALLOCATABLE, ASYNCHRONOUS, OPTIONAL, POINTER, TARGET, VALUE, or VOLATILE attribute,
    (b) is an assumed-shape array,
    (c) is a coarray,
    (d) is of a parameterized derived type, or
    (e) is polymorphic,
  3. the procedure has a result that
    (a) is an array,
    (b) is a pointer or is allocatable, or
    (c) has a nonassumed type parameter value that is not a constant expression,
  4. the procedure is elemental, or
  5. the procedure has the BIND attribute.

Beyond those, an implicit interface will be sufficient and the interface block not required. But could still helpful.

In your case, it's only for the calls between the various Fortran components where interfaces have this impact. So, you don't always need to write interfaces and some things can work without them.

like image 114
francescalus Avatar answered Jul 09 '26 10:07

francescalus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!