I'm not sure how general this behaviour is with respect to other types, but 
I have observed the following with a simple composite type:
 
When summing a 1-element array of a simple composite type, the return is a 
reference to the single element, rather than a copy of the single element.
This seems at odds with the return value of summing an array of any other 
size (even 0-element array if zero is defined)
Is this the desired behaviour?
 
 
type MyType
   x::Int
end
+(a::MyType, b::MyType) = MyType(a.x + b.x)
A = [ MyType(i) for i = 1:5 ]
sumA = sum(A)
A[1].x = 77
sumA # MyType(15), seems reasonable
 
B = [ MyType(i) for i = 1 ]
sumB = sum(B)
B[1].x = 88
sumB # MyType(88), is this reasonable?
sum(B) === B[1] # true
 
 
I guess for a copy  to be returned (rather than a reference) would need to 
define how to copy. Perhaps require on of the following?  

   - copy constructor
   - copy() function
   - zero() and return A[1] + zero()

 
Would this be preferable to returning a reference?
 
If I want to return a copy of single element, do I need to define my own 
sum() function instead?
 

Reply via email to