-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
zeros
etc. is arguably broken for mutable element types
#29168
Comments
This might be ok to change, but can it really be enough? Do we have to make a rule that you must never write This also has a performance cost. We don't want to allocate a new number for each element for |
That's completely opposite my expectation here, but perhaps that because I've touched the implementation. I expect |
If that's the implementation of Now, I understand the
This is job of the caller, isn't it? If they want the same instance in every element, that's what |
If the consensus ends up being that this is the expectation, and I'm just the odd one out, then we can just document it and move on. |
I agree it feels funny in the case of JuMP that you linked… but I don't think it'd be terribly offensive for JuMP to add a |
To me the term "zero" implies that something is numeric, which in turn implies that its identity should be determined by its value and it probably shouldn't be mutable. It's fine if it happens to be mutable but behave in a reasonable way for numbers (like BigInts, for example) but I think that being able to mutate zero is the real root of the problem here. |
Yeah, JuMP already has the problem that it attempts to use scalars' types to dictate array behavior, which makes this annoying to implement without a (needed, IMO) refactor of the JuMP code, but that's besides the more general problem.
Agreed. Base's numeric interface in general has a lot of unstated assumptions like these. |
Nobody wants an array of BigInts that uses enormously more memory than it needs to. I'll point out that during 0.7 development ones and zeros were almost deprecated to
It's neither --- it's just random code somewhere, where somebody is making an array of two zeros. The point is that
The idea that zeros (and numbers generally) are immutable is not just some random unstated assumption. It is nearly impossible to do any mathematical or numerical programming with "mutable numbers" that always allocates and/or shares references as somebody expects. If this behavior of |
You asked me whether I thought people should never write that code. My answer was "it depends on what the intention of the code is" and I gave examples that seemed reasonable to me.
I'm not arguing for that at all. Just that a function's behavior be well-defined in the documentation (with a preference towards a certain behavior, but deference towards the consensus opinion).
I never said it was random, I'm not even saying it's unreasonable. It is unstated. AFAIK (apologies if I'm wrong here), there's no documentation telling me that generic numeric code in Base relies on the assumption that my custom number types are immutable and/or don't maintain references to mutable state.
This is quite incorrect. In other languages especially, but also in Julia, it is an extremely common way to implement various for numerical programming techniques, especially those that require graph accumulation/code interception. Obviously, I hope that new approaches using tools like Cassette will be better for such purposes, but this is the way things have been implemented in Julia for years now, JuMP being the obvious example.
We did write a custom function. We don't need to use I'm not trying to ruffle anybody's feathers, here. Apologies if I did. |
Ref #27483, regarding |
But somebody who writes I can imagine that "mutable number"-based techniques might in fact work (though perhaps give slightly different answers) no matter how |
That is generally the case, if the implementation is sound. Just to clarify: this issue was not intended to be about the viability of I was just pointing out that I thought I could use |
My two cents: I got quite confused in the past with the behavior of zeros with mutable types, since I expected what @jrevels says. I might be biased, because I am a Python programmer and therefore tend to associate Julia's I see not much progress has been made on this side, but perhaps it is worth reconsidering this issue in the near future? |
I don't know how we would document that Mostly just echoing Stefan's comment above #29168 (comment) |
Apologies if dup.
I would've expected each element to have been instantiated separately via a new
zero
call. If I wanted this behavior, I would've just calledfill(zero(Foo), 2)
.see jump-dev/JuMP.jl#1456 (comment)
obligatory xref to #24595
The text was updated successfully, but these errors were encountered: