Function with no arguments, but with type in Julia

I recently looked through some of Julia's source codes and found that some features seem cryptic to me.

There is some function defined in Julia's source code where they have no arguments, but there are type annotations.

For example: line 20 in abstractarray.jl

I'm trying a little bit to execute a function ndims,

it seems that it ndimscan take the type itself as an argument and return the correct value:

julia> ndims(AbstractArray{Float64, 2})
       2
julia> ndims([1.1 0.3; 0. 0.5])
       2

Can someone explain to me how the (::DataType)methods work? Or what does this mean in Julia?

+4
source share
1 answer

Julia , . , , ndims, - . , ndims, @which , :

julia> @which ndims(AbstractArray{Float64, 2})
ndims{T,n}(::Type{AbstractArray{T,n}}) at abstractarray.jl:61

julia> @which ndims([1.1 0.3; 0. 0.5])
ndims{T,n}(::AbstractArray{T,n}) at abstractarray.jl:60

abstractarray.jl :

ndims{T,n}(::AbstractArray{T,n}) = n
ndims{T,n}(::Type{AbstractArray{T,n}}) = n

{T,n}.

  • AbstractArray{T,n} - , [1.1 0.3; 0. 0.5] ( ).
  • AbstractArray{T,n}.

( , , , , , )

types methods Julia.

+10

All Articles