-
Notifications
You must be signed in to change notification settings - Fork 53
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
Violation of the (informal) Number
interface
#331
Comments
Discussion related to #224 might be relevant here. |
Yeah, that issue did bring up this particular issue, but has been closed without the actual issue being resolved :/ From #224
The benefit of respecting the Number interface is correctness, that you can pass |
I agree with your point as well as that in #224. Let me first say that the fact that we have that Now, in your example, |
I understand that it is convenient, but the implication is that you cannot pass I was facing similar problems in MonteCarloMeasurements.jl where indexing was used to represent a sample of a quantity that behaved like a number. After having encountered silently incorrect results a number of times I concluded that this was really dangerous and had to abandon the indexing convenience. My documentation still has this remainder to this day: |
I truly see your point, and indeed convenience is the property challenged here. Yet, you can argue just the same but in the opposite direction, about a program which uses After saying this, let me argue that the fact that I guess this discussion boils down to "a matter of taste" (and tastes are not discussed 😄): If I would like that a function works with vectors and scalars, I would exploit (multiple) dispatch, in the sense of writing two or more functions that differ in some types; your choice is to have a single function that works universally. |
The problem is that my taste does not matter if I call a function inside a library I have not written. To make use of The problem extends to iteration as well: julia> t = TaylorN(1)
1.0 x₁ + 𝒪(‖x‖⁷)
julia> for i in t
println(i)
end
0.0
1.0 x₁
0.0
0.0
0.0
0.0
0.0
julia> for i in 1
println(i)
end
1
This is not only my choice, there is an infamous PR that tried to change this behavior so that scalars were not iterable and indexable, but so much of Base julia relied on this behavior that it was abandoned. It has been observed many times that this functionality is used all over the place and any time a |
From JuliaLang/julia#19700, my take-away is to avoid indexing scalars. We do index though, with the meaning it is described, and this is explained in the documentation. Again, this is a matter of taste, in this case, our tastes are distinct. Sorry for the inconveniences. |
I have been hitting my head at #330 for quite some time now, and I just realized that it's due to the type
TaylorN
claiming to be a<: Number
, but deviates from the interface forgetindex
for numbers:For any scalar
x::Number
, the operationsx[]
andx[1]
are expected to be identical, but forTaylorN
they aren't.The text was updated successfully, but these errors were encountered: