Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
921 views
in Technique[技术] by (71.8m points)

fortran - Why should I use interfaces?

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?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...