j3-fortran / generics

BSD 3-Clause "New" or "Revised" License
36 stars 8 forks source link

Why has the chosen system resulted in requiring manual instantiation? #107

Open dacarnazzola opened 10 months ago

dacarnazzola commented 10 months ago

This seems like a consequence of choosing to parameterize entire modules (and permitting nesting of templates) rather than the approaches taken by other modern languages (see the comparison to Go, Rust, Haskell).

What was the driving motivation behind such a system design? I find the manual instantiation of templates absolutely terrible for users of the language, and showing a seeming disregard for those that came before. Go, Rust, and Swift all have generic systems with "strong concepts," based around common interfaces and composition. Yet, the proposal for Fortran looks most similar to C++ templates, the only system brought up as a bad example (specifically due to "no concepts," followed by "concepts light").

This has been a large group effort by many experienced, well educated, and well meaning people - so I am confused what logic lead us to the current point.

ivan-pi commented 5 months ago

What was the driving motivation behind such a system design? I find the manual instantiation of templates absolutely terrible for users of the language, and showing a seeming disregard for those that came before.

I haven't been involved in this effort, but I believe the motivation was to prevent the long error messages that can result from template instantiation. Instead the (in)validity of template is supposed to be easily detectable, so that an understandable error message can be given.

ivan-pi commented 5 months ago

What was the driving motivation behind such a system design? I find the manual instantiation of templates absolutely terrible for users of the language, and showing a seeming disregard for those that came before.

I haven't been involved in this effort, but I believe the motivation was to prevent the long error messages that can result from template instantiation. Instead the (in)validity of template is supposed to be easily detectable, so that an understandable error message can be given. Hence, it falls on the user to specify exactly which procedure they want.

Personally, I'm still undecided whether explicit instantiation is a good or bad decision. It seems like with a few minor modifications the syntax could allow automatic type deduction:

program main
  use reverse_module, only: reverse => reverse_tmpl(*) :: reverse   ! (*) as generic type place-holder
  !use reverse_moduly, only: reverse_tmpl
  !instantiate reverse_tmpl(integer), only: reverse
  integer :: a(5)
  a = [1,2,3,4,5]
  call reverse(a)   ! deduced reverse_tmpl(integer) :: reverse
  print *, a
end program

Since C++17, C++ allows programming user-defined deduction guides, to help the compiler in cases which are ambiguous, or when implicit instantiation is simply not possible.