-
Notifications
You must be signed in to change notification settings - Fork 178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Increment #541
Comments
I like it. Some ideas:
What module would this belong to? |
In practice I would prefer to implement this as two functions to avoid an
|
I wonder if this is necessary... If speed is crucial and the compiler fails to optimise this At the end of the day it will not matter if we implement this with an |
There is a difference in the API between interfaces and a procedure with an optional argument. For example integer :: val
integer, allocatable :: step
val = 1
call incr(val, step) would be perfectly valid with the optional argument strategy, while it produces an uninitialized memory access with interfaces. A similar situation occurs if user code passes an optional argument to the second argument. The workaround for interface based implementation for those two cases would be call incr(val, optval(step, 1)) This are arguably corner-cases seldom encountered for a simple function like |
It could go into the same submodule as |
I am aware of the thread which showed little to no performance penalty of A discussion of interface resolution vs optional arguments would be a good topic for the Fortran Best Practices minibook. |
This thread showed little or no performance penalty if link-time optimization is used ( From recent experience it can raise compiler bugs -- for example here. Also, I seem to recall once finding that I couldn't use lto when linking to another library (netcdf) that had been compiled without lto. I'm not opposed to these syntatic-sugar type routines, but agree care is required to manage the trade-offs, including when promoting one or other coding-style. |
Maybe we can follow the style of the intrinsic functions :). For example, try the following codes. program main
implicit none
call test_optional(1)
call test_optional(2)
call test_optional()
contains
subroutine test_optional(a)
integer, intent(in), optional :: a
integer :: b(3,5)
print*, size(b,dim=a)
end subroutine
end program main
program main
implicit none
call test_allocatable()
contains
subroutine test_allocatable()
integer, allocatable :: a
integer :: b(3,5)
print*, size(b,dim=a)
end subroutine
end program main
If we ignore the results of ifx (beta), |
I'm having difficultly seeing the benefit of using To me this situation might be better solved by pushing for the addition of |
You might be right about the |
Even in that case there isn't a benefit as you still have to make the subroutine call to |
I want to increment the field, not |
I can see why in that case there would be benefit to an increment subroutine if there were no other solution to simplifying the surrounding logic, but I suspect there may be better alternatives to avoiding that situation to begin with. If it is truly only the one time (making a reworking of the surrounding logic a waste of time),
Is not any more difficult to read that you dealing with something that nested to begin with. In such (hopefully very few) cases that it cannot be avoided and the need to make increment logic exhaustively clear, an increment subroutine takes very little time to write and has very little risk of being written incorrectly. I don't want to lose sight of my main point though. I am putting forward that I think there is more harm than good in adding an increment subroutine especially with one that will take up another short and potentially useful (depending on the field and context) variable name. Especially when it is only useful in what I believe would be edge cases in well constructed code logic. And additionally it doesn't fully solve what I would see as the whole problem. While as described it could solve decrementing, I think using |
Many other languages allow
i += 1
instead ofi = i + 1
. When you are incrementing a variable with a long name, or an array section, or a component of a derived type, it is possible to writei = j + 1
by mistake, where it was intended thati
be incremented. Therefore I suggest that stdlib have an elementalincrement
subroutine that works with real and integer types, so thatcall increment(i,inc)
has the same meaning as
i = i + inc
I got this idea from Nim, where you can write
inc(i,1)
instead ofi = i + 1
The text was updated successfully, but these errors were encountered: