JuliaLang/julia — pull request #1269 by carlobaldassi (2012-09-09 16:15:42+00:00)
Title: deep copying Functions (and allow overriding)

I changed my mind about deep-copying Functions: I think they should be deep-copied (explanation below), but even though I couldn't find any issue with doing that, I'm not 100% sure that it's completely safe, and that's the reason for the pull request.

The patch also allows overriding the default behaviour, exposing ``deepcopy_internal`` for that purpose. I'm not convinced about that name, but couldn't think of anything better.

The reason for deep-copying functions is that closures won't work properly otherwise. Example:

```
julia> type P
i::Int
incr::Function
function P(i::Int)
p = new(i)
p.incr = ()->(p.i += 1)
p
end
end

julia> p = P(0); p.i
0

julia> p.incr() # ok, not my favourite idiom either...
1

julia> q = deepcopy(p);

julia> q.incr() # increments p rather than q
2

julia> p.i
2

julia> q.i
1
```

The patch makes this work (just by removing Functions from the special group of immutables).