-
-
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
convert can lead to unexpected aliasing #12441
Comments
Potentially relevant sub-issue: should a |
We use copy{T}(::Type{T}, x::T) = copy(x)
copy{T}(::Type{T}, x) = convert(T, x) The caveat of the second method is that for some composite types, |
On second thought, |
See #8759 for a discussion of our policy on aliasing. |
The |
Reopening per @StefanKarpinski's comment. I looked for this issue (again) after reading this code in GLM.jl: wts = T <: Float64 ? copy(wts) : convert(typeof(y), wts)
off = T <: Float64 ? copy(offset) : convert(Vector{T}, offset) I can see two solutions:
Note that the former already exists in Base, it's called |
I think the right way to write this is |
Whether to extend On the one hand, forming the copy-of-type operation via composition (and thereby avoiding interface expansion) is elegant. On the other hand, relying on a mechanism still somewhat shrouded in ambiguity ( Thoughts? Best! |
That doesn't work very well at all for immutable (container) types, like those in +1 for |
Why would you need to copy an immutable container? |
Ideally, I would like methods written by other people for (EDIT: e.g. a |
Ah yes, good old "defensive copies". I think it would be better to check for mutability in the relatively rare cases where that's wanted, instead of requiring all immutable collections to implement |
Also, in static arrays it might make sense to use
A trait for array mutability would be quite helpful! |
In the interest of "generic programming" (whatever the precise definition of that is) I know of these possibilities in this space:
|
We use |
I guess the idea might be to make something like |
Another approach to fixing this would be to introduce a Often, most of the code needs to be common to both methods, except that to return a copy you need to call We could also default to copying, for safety, and consider EDIT: another advantage of this approach is that it could be used more generally by other functions, and in particular by constructors (for which we have no clear policy regarding aliasing AFAIK). |
Tentative bump on this, with 1.0 in mind. I'm not sure if there is any more action possible than the documentation change that was done at the time, but would seem to be last call on any possible "fix" (or actually past it). |
In 1.0, replacing |
There's also now
|
Consider:
The input is modified in one case, and not the other, which is in the realms of "scary". One (The?) solution is to always do a copy, even if the type matches, for mutable types.
The text was updated successfully, but these errors were encountered: