Skip to content

Commit

Permalink
Add generic copy stagedfunction for all types
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mbauman committed Dec 8, 2014
1 parent dd03b5e commit e4b47ac
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 8 deletions.
2 changes: 0 additions & 2 deletions base/expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ copy(e::Expr) = (n = Expr(e.head);
n.args = astcopy(e.args);
n.typ = e.typ;
n)
copy(s::SymbolNode) = SymbolNode(s.name, s.typ)
copy(n::GetfieldNode) = GetfieldNode(n.value, n.name, n.typ)

# copy parts of an AST that the compiler mutates
astcopy(x::Union(SymbolNode,GetfieldNode,Expr)) = copy(x)
Expand Down
1 change: 0 additions & 1 deletion base/linalg/uniformscaling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ ndims(J::UniformScaling) = 2
getindex(J::UniformScaling, i::Integer,j::Integer) = ifelse(i==j,J.λ,zero(J.λ))

show(io::IO, J::UniformScaling) = print(io, "$(typeof(J))\n$(J.λ)*I")
copy(J::UniformScaling) = UniformScaling(J.λ)

transpose(J::UniformScaling) = J
ctranspose(J::UniformScaling) = UniformScaling(conj(J.λ))
Expand Down
12 changes: 9 additions & 3 deletions base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,15 @@ widen{T<:Number}(x::T) = convert(widen(T), x)

sizeof(x) = Core.sizeof(x)

# copying immutable things
copy(x::Union(Symbol,Number,AbstractString,Function,Tuple,LambdaStaticData,
TopNode,QuoteNode,DataType,UnionType)) = x
# copying immutable things is a no-op, mutable things create a new object with the same contents
stagedfunction Base.copy(x)
if !x.mutable
:x
else
args = map(fld->:(x.$fld), x.names)
Expr(:call, x, args...)
end
end

# function pipelining
|>(x, f::Callable) = f(x)
Expand Down
1 change: 0 additions & 1 deletion base/process.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ end

immutable DevNullStream <: AsyncStream end
const DevNull = DevNullStream()
copy(::DevNullStream) = DevNull
uvhandle(::DevNullStream) = C_NULL
uvhandle(x::Ptr) = x
uvtype(::Ptr) = UV_STREAM
Expand Down
2 changes: 1 addition & 1 deletion base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ maximum(r::Range) = isempty(r) ? error("range must be non-empty") : max(first(r
ctranspose(r::Range) = [x for _=1, x=r]
transpose(r::Range) = r'

# Ranges are immutable
# Ranges are immutable, but the AbstractArray copy method doesn't know that
copy(r::Range) = r


Expand Down

0 comments on commit e4b47ac

Please sign in to comment.