You can do something like this:

C:\Users\Ismael                                                             
             
λ julia5                                                                   
              
               _                                                           
              
   _       _ _(_)_     |  By greedy hackers for greedy hackers.             
             
  (_)     | (_) (_)    |  Documentation: http://docs.julialang.org         
              
   _ _   _| |_  __ _   |  Type "?help" for help.                           
              
  | | | | | | |/ _' |  |                                                   
              
  | | |_| | | | (_| |  |  Version 0.5.0 (2016-09-19 18:14 UTC)             
              
 _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org/ release           
              
|__/                   |  x86_64-w64-mingw32                               
              
                                                                            
             
julia> Base.sum{T<:Real, S<:Real}(::Type{T}, xs::Range{S})::T = sum(xs)     
             
                                                                            
             
julia> r = 1:100; sum(r), sum(Int32, r), sum(Rational{Int16}, r), 
sum(Complex{BigInt}, r)
(5050,5050,5050//1,5050 + 0im)                                             
              
                                                                            
             
julia> for T ∈ (Int32, Rational{Int16}, Complex{BigInt})                   
              
           @assert sum(T, 1:100) |> typeof == T                             
             
       end                                                                 
              
                                                                            
             
julia>                                                                     
              



El lunes, 17 de octubre de 2016, 8:19:51 (UTC-5), Ángel de Vicente escribió:
>
> Hi, 
>
> probably a very basic question, but I'm just starting to play around 
> with Julia types. 
>
> I was hoping to improve the performance of a little program I wrote, so 
> I decided to try Int32 integers instead of the default Int64, but if I 
> try to use sum, it seems that it is expecting Int64 and the result is 
> Int64, defeating the purpose of working with Int32 and actually making 
> the code much slower than the naive version. 
>
> ,---- 
> | julia> typeof(sum([i for i in Int32(1):Int32(100)]))                     
>                                                                             
>                                                            | 
> | Int64 
> `---- 
>
> Do I have to write down my own Int32::sum function? I assume I'm missing 
> something quite obvious? 
>
> Thanks, 
> -- 
> Ángel de Vicente 
> http://www.iac.es/galeria/angelv/           
>

Reply via email to