Define Base.copy for ::AbstractTime#9246
Conversation
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?
|
cc @quinnj |
|
Why don't we have a generic fallback |
|
I was wondering that myself. |
|
We could use staged functions to write really efficient fallbacks for |
|
+1 for efficient fallbacks Could staged functions be used to make a generic |
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 |
|
I think we |
|
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). |
|
|
|
The default |
|
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. |
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
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
This comes up when specifying the plot bounds in Dates (e.g., in GiovineItalia/Gadfly.jl#354). I suppose defining this for all
::AbstractTimeis 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?