Function objects in Fortran

I certainly understand your desire. Overloading the operator() is a very common pattern in C++ (an example can be seen in my post on C++ MEX functions) and Python.

Most of the interpolator objects in scipy.interpolate provide a __call__ method to give the interpolants a function-like interface, e.g.

from scipy.interpolate import BarycentricInterpolator

y = BarycentricInterpolator([0,1,2],[0,1,4])
xi = np.linspace(0,2,50)
yi = y(xi)

For Fortran derived types I typically use the name %eval for this purpose. But as you mentioned earlier this is best considered separately from the case of container/array access.

Concerning containers, a project currently on hold is to provide a C++ container interface to Fortran arrays (Fortran/C++ compatibility library · Issue #325 · fortran-lang/stdlib · GitHub). This would make it easy to use C++ iterators and algorithms to operate on Fortran arrays. You could then implement your algorithm in C++.

2 Likes