Thanks, I thought about that. That is why the method definition I wrote
works like `scale` (which is deprecated), rather than `scale!`.
This is more of a programming problem rather than a conceptual one.
Probably, I could just do something along the lines of
if isa(out, Number)
out *= 42
else
scale!(out, 42)
end
On Thursday, June 23, 2016 at 6:42:41 PM UTC+1, Stefan Karpinski wrote:
>
> This method cannot be implemented since numbers are immutable.
>
> On Thu, Jun 23, 2016 at 1:20 PM, Davide Lasagna <[email protected]
> <javascript:>> wrote:
>
>> I have a function that operates on an AbstractVector object `fs`, defined
>> like this:
>>
>> function foo{T}(fs::AbstractVector{T})
>> out = zero(T)
>> for f in fs
>> # do some with out and f, e.g. sum
>> end
>> scale!(out, 42)
>> end
>>
>> This code should be generic, and the eltype `T` could be a number, e.g.,
>> a Float64, or a vector, e.g. Vector{Float64}. The function loops over the
>> entries of `fs` and then scales the `out` variable by a magic number 42.
>>
>> The above definition works well when `T` is a vector, but fails when `T`
>> is a scalar with a MethodError, as scale! does not have a method when both
>> inputs are numbers. A possible method definition would be something along
>> the lines of
>>
>> scale!(a::Number, b::Number) = a*b
>>
>> Probably `scale!` is not the right way to implement this. Any suggestions
>> that apply to 0.4 and/or 0.5 are very welcome!
>>
>> Thanks
>>
>>
>