-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Should copy
be renamed shallowcopy
?
#42796
Comments
Perhaps adding a “See also: I know, these doc-strings appear in the Perhaps even add an example?
|
We should not encourage anybody to call |
Given that this agrees with my own coding practice, can't say I disagree. It provokes the question of how widely it is used: on my tim@diva:~/.julia/dev$ grep -Rc deepcopy | grep -v ":0$" | grep "test/" | wc
40 40 1528
tim@diva:~/.julia/dev$ grep -Rc deepcopy | grep -v ":0$" | grep "src/" | wc
90 90 3194 That's a very small fraction of all source files, but to my surprise it's more widely used in The question remains, though: what more can we do to clarify how Lines 358 to 366 in 3d4b213
probably can be called "non-newbie friendly" since it relies on understanding the implications of "shallow" and "identically-same." Those are great if you already understand the distinction between == and === or the jargon of shallow vs deep, but I can attest that's something that a lot of capable users from non-CS fields don't know coming into Julia. Of course, my proposal to rename this shallowcopy is not successful in that either, other than directing attention to the fact that this may be more subtle than a totally-naive perspective might imagine.
Let me see if I can come up with something clearer (in a few days), and others can chime in. |
I think the first sentence is fine, it's just the second that's not very exemple-y. Maybe just "For example, |
While we are improving the docstring of Jargon such as “shallow” and “deep” should be introduced, not assumed. The documentation of these functions is also complicated by the fact that Julia's memory model is not at all obvious from its syntax, e.g. mutable and immutable structs look exactly the same except for a keyword, but may be treated quite differently in a “shallow” copy. I wonder if it wouldn't be useful to add a visualization tool that shows what |
You need to bind these values to names, such that you can reference them. Otherwise the example does not have any observable outcomes (and in fact the optimizer would be entitled to eliminate it). So you can't make that example much shorter than
|
While it doesn't yet address the docstring issue, I wrote a lengthy explanation in my course documents: https://github.com/timholy/AdvancedScientificComputing/blob/main/homeworks/learning_julia2.md#distinguishing-identity-and-equality-and-containers-and-their-collection-of-items I'd be happy to move that to Julia's docs somewhere if that seems desirable. Maybe the FAQ? Do we want an "Explanations" section that covers basic concepts of computing using Julia as the language? As I start thinking about teaching Julia even to bare-beginners, referencing C documentation or even Python to explain these core concepts starts becoming less and less satisfying. OTOH, one might say that this material should really just go in textbooks and doesn't belong anywhere in this repo. |
Noob reporting in! Just out of curiosity, if I have a struct that is somewhat deep and complex but with no loopy data structure and I want to make a copy of it to have two separate copies to manipulate in different ways, if not just copying it with deepcopy, towards what kind of thinking would you point me? |
You can create custom struct MyStruct
uuid::UUID # the unique identifier for this particular instance
...
end might need to specialize Whether it's OK to specialize copy to nested types is a little more confusing. Personally I think we're a bit inconsistent about the shallow issue, for example Lines 37 to 64 in c762f10
|
I'm generally a bit restrictive about creating versions of methods from Julia Base for my own types since I've noticed it can come back to bite me. Sure getindex, show and iterators etc are just too handy to leave on the table but in the case of a custom deepcopy for example I have chosen to give that a new name. But this is a more general discussion in Julia land i feel. |
While on the topic, could you comment on whether or not the intended meaning of copy agrees with its use in
To me at least this is surprising; I expect copy to keep the object type invariant. |
This is a potential Julia 2.0 change that arose while teaching newbies. One student got bit by
copy
's shallow behavior; this is also a common issue on discourse and stackoverflow. (Those are examples, and there are duplicates.) A potential solution is to not havecopy
at all, instead offering a choice betweenshallowcopy
ordeepcopy
.This has downsides: (1)
shallowcopy
is longer to type, (2) other languages (e.g., Python) make the same naming choices we're currently making, and (3) it might cause more people to usedeepcopy
out of safety in cases where it's not actually necessary (deepcopy
is much slower).The main upside is that by naming it
shallowcopy
we encourage people to understand what it's doing and thus to think about whether they want shallow or deep in this particular instance. Bugs arising from a shallow copy can be quite difficult to diagnose, typically costing orders-of-magnitude more time than typing a few extra characters.A downside-to-the-upside is that this isn't unique to
copy
:fill
, pass-by-reference, and other circumstances can trigger what is essentially the same confusion, and I don't see as simple a solution for these.The text was updated successfully, but these errors were encountered: