Yes, this is possible. A common Julian pattern is:
function foo!(dest::Array, src::Array)
mutate!(dest, src)
end
function foo(src::Array)
dest = copy(src)
foo!(dest, src)
return dest
end
Some other points to note:
Array{FloatingPoint} isn't related to Array{Float64}. Julia's type system
always employs invariance for parametric types:
https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)
The numeric parameter of an Array type is its tensor order, not its number of
elements. So it's rare you'd work with Array{FP, 5}.
-- John
On Aug 25, 2014, at 10:16 AM, Roy Wang <[email protected]> wrote:
> I have some ideas from my experience in C++11, but I'd like to learn the
> proper "Julian" way :)
>
> My goal is to implement a function that computes an Array. I do this if I
> want speed:
>
> function computestuff!(A::Array{FloatingPoint,1})
> A=fill!(A,0); #reset array values
> #modify the contents of A
> end
>
>
> #in the calling routine...
> A::Array{FloatingPoint,1}=Array(FloatingPoint,5);
> computestuff!(A);
>
> Question 1: Is there an even faster way in Julia?
>
>
> Question 2: I wish to hide the details inside that function (i.e. allocate
> the size of A inside of the function) without sacrificing speed. This way, in
> the calling routine, I can just write
>
> #in the calling routine...
> A=computestuff(5);
>
>
> Is this possible?
> I think any new memory allocated inside that function will be undefined/freed
> once the function exits. If the pointer A is assigned to this memory, I'd get
> undefined results. Julia probably checks against this kind of situation and
> assigns a deep-copy instead, which slow things down.
>
> Thanks!
>
>