Skip to content
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

Define Base.copy for ::AbstractTime #9246

Closed
wants to merge 1 commit into from

Conversation

mbauman
Copy link
Member

@mbauman mbauman commented Dec 4, 2014

This comes up when specifying the plot bounds in Dates (e.g., in GiovineItalia/Gadfly.jl#354). I suppose defining this for all ::AbstractTime is a little dangerous as others could define their own mutable subtypes. I could easily change this into a large union if desired. But I think, in general, there should be a copy method for these types, no?

This comes up when specifying the plot bounds in Dates (e.g., in GiovineItalia/Gadfly.jl#354).  I suppose defining this for all `::AbstractTime` is a little dangerous as others could define their own mutable subtypes. I could easily change this into a large union if desired.  But I think, in general, there should be a copy method for these types, no?
@mbauman
Copy link
Member Author

mbauman commented Dec 4, 2014

cc @quinnj

@ivarne
Copy link
Member

ivarne commented Dec 4, 2014

Why don't we have a generic fallback copy function? Just noop for immutable and constructing a new object with the same properties for types.

@quinnj
Copy link
Member

quinnj commented Dec 4, 2014

I was wondering that myself.

@JeffBezanson
Copy link
Member

We could use staged functions to write really efficient fallbacks for copy and == (e.g. #4648).

@ivarne
Copy link
Member

ivarne commented Dec 4, 2014

+1 for efficient fallbacks

Could staged functions be used to make a generic deepcopy that work without a dict in the cases where no self-referential is possible?

@mbauman
Copy link
Member Author

mbauman commented Dec 5, 2014

Why don't we have a generic fallback copy function? Just noop for immutable and constructing a new object with the same properties for types.

Can we assume that all user-defined types have the default constructor (or something with an equivalent signature and functionality)? Otherwise I don't know how to generically construct a type. I've not played with stagedfunctions much, but this seems to do the trick.

julia> stagedfunction Base.copy(x)
         if !x.mutable & x.pointerfree # Should only pointerfree immutables be no-ops?
           :x
         else
           args = map(fld->:(copy(x.$fld)), x.names) # Edit: this approach wouldn't allow for circular references
           Expr(:call, x, args...)
         end
       end

@ivarne
Copy link
Member

ivarne commented Dec 5, 2014

I think we can should require a suitable default constructor for the default copy to work.

@mbauman
Copy link
Member Author

mbauman commented Dec 5, 2014

I'm not sure how you'd handle circular references to the object within its fields, though. I suppose we could do the Matlab thing and require all types to have a zero-argument constructor function (ducks).

@ivarne
Copy link
Member

ivarne commented Dec 5, 2014

The efficient deepcopy would only work when the type's fields is fully typed (eg. bits-types and arrays of those), so that we can statically determine that no cycles are possible.
Sorry didn't understand.

@ivarne
Copy link
Member

ivarne commented Dec 5, 2014

The default copy can fail (with a recommendation to use deepcopy) if one of the types fields can't be proven (statically) to not contain cycles. There might also be an argument that we shouldn't make copies of the fields in the first place, as this is consistent with the array behavior.

@mbauman
Copy link
Member Author

mbauman commented Dec 8, 2014

Right. Of course. This will recurse. I didn't think about this very clearly. Going by the help text ("outer structure is copied") and the Dict/Array implementations of copy, it seemed like the fields should get copied… but I guess that will be dependent upon the data structure, and so the generic copy should probably be conservative.

mbauman added a commit to mbauman/julia that referenced this pull request Dec 8, 2014
It is a no-op for all immutable types, and tries to call the default constructor for mutable types.

Supersedes JuliaLang#9246; see that issue for some discussion of this
mbauman added a commit to mbauman/julia that referenced this pull request Dec 8, 2014
It is a no-op for all immutable types, and tries to call the default constructor for mutable types.

Supersedes JuliaLang#9246; see that issue for some discussion of this
@mbauman mbauman closed this Dec 8, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants