Nice talk by D. Rouson on modern Fortran features

I think it’s flang-new. That (classic) flang you showed doesn’t seem to support F2008’s recursive derived types using allocatable components —POINTERs must be used, as mentioned by the error message.

Something similar applies to older versions of gfortran.

$ flang --version
Homebrew flang version 20.1.8
Target: arm64-apple-darwin24.6.0
Thread model: posix
InstalledDir: /opt/homebrew/Cellar/flang/20.1.8/libexec
Configuration file: /opt/homebrew/Cellar/flang/20.1.8/libexec/flang.cfg
Configuration file: /opt/homebrew/etc/clang/arm64-apple-darwin24.cfg

These linked lists can be setup in several ways, but fortran allows the use of allocatable arrays and derived types, which simplifies many operations. For example, to delete the entire linked list, you just need to do

deallocate( list )

and the compiler traverses the whole list with no chance of memory leaks or dangling pointers. You can also define a pointer that traverses the list in a straightforward way. I used the move_alloc() intrinsic to add to the list, members can also be removed from the list that way. The programmer has lots of options with fortran.

Here’s my pointer based linked list implementation: flt/src/nmllog.f90 at 2a08a9061ccb337df9543228d3135d0332aaa3f8 · btrettel/flt · GitHub

As I recall, my linked list implementation was based on the linked list example in this book: Fortran 90 Programming (International Computer Science Series): Ellis, T. M. R., Philips, Ivor R., Lahey, Thomas M.: 9780201544466: Amazon.com: Books

Ron Shepard’s allocatable approach is better as it’s safer and simpler. I didn’t know about that back when I wrote my logger. I think I first learned about allocatable linked lists when reading the Intel Fortran Compiler forum in the past year or two. Another mistake I made was that I made it too complicated by having separate impure and pure loggers. Eventually, I will rewrite my logger to fix these issues.

If I remember correctly, all of the recent editions of Modern Fortran Explained have a doubly connected generic link list example.

Yes. In the latest edition it is the file oo.f90 in the zip file available, free, on the ‘codes’ button at https://global.oup.com/booksites/content/9780198876571/ . Its description is in Appendix C of the book itself.

Mike Metcalf

1 Like

flang 21.1.0 has made it to Homebrew.

3 Likes