Function overloading instead of dot syntax

Hey, i have created this function and I would like to overload the last function (vdw_radius) instead of using .@ macro. Any help?

 ​_vdw_radius_crc ​=​ [ 
 ​    ​110.0​, 
 ​    ​140.0​, 
 ​    ​182.0​, 
 ​    ​153.0​, 
 ​    ​192.0​, 
 ​    ​170.0​, 
 ​    ​155.0​, 
 ​    ​152.0​, 
 ​    ​147.0​, 
 ​    ​154.0​, 
 ​    ​227.0​, 
 ​    ​173.0​, 
 ​    ​184.0​, 
 ​    ​210.0​, 
 ​    ​180.0​, 
 ​    ​180.0​, 
 ​    ​175.0​, 
 ​    ​188.0​, 
 ​    ​275.0​, 
 ​    ​231.0​, 
 ​    ​215.0​, 
 ​    ​211.0​, 
 ​    ​207.0​, 
 ​    ​206.0​, 
 ​    ​205.0​, 
 ​    ​204.0​, 
 ​    ​200.0​, 
 ​    ​197.0​, 
 ​    ​196.0​, 
 ​    ​201.0​, 
 ​    ​187.0​, 
 ​    ​211.0​, 
 ​    ​185.0​, 
 ​    ​190.0​, 
 ​    ​185.0​, 
 ​    ​202.0​, 
 ​    ​303.0​, 
 ​    ​249.0​, 
 ​    ​232.0​, 
 ​    ​223.0​, 
 ​    ​218.0​, 
 ​    ​217.0​, 
 ​    ​216.0​, 
 ​    ​213.0​, 
 ​    ​210.0​, 
 ​    ​210.0​, 
 ​    ​211.0​, 
 ​    ​218.0​, 
 ​    ​193.0​, 
 ​    ​217.0​, 
 ​    ​206.0​, 
 ​    ​206.0​, 
 ​    ​198.0​, 
 ​    ​216.0​, 
 ​    ​343.0​, 
 ​    ​268.0​, 
 ​    ​243.0​, 
 ​    ​242.0​, 
 ​    ​240.0​, 
 ​    ​239.0​, 
 ​    ​238.0​, 
 ​    ​236.0​, 
 ​    ​235.0​, 
 ​    ​234.0​, 
 ​    ​233.0​, 
 ​    ​231.0​, 
 ​    ​230.0​, 
 ​    ​229.0​, 
 ​    ​227.0​, 
 ​    ​226.0​, 
 ​    ​224.0​, 
 ​    ​223.0​, 
 ​    ​222.0​, 
 ​    ​218.0​, 
 ​    ​216.0​, 
 ​    ​216.0​, 
 ​    ​213.0​, 
 ​    ​213.0​, 
 ​    ​214.0​, 
 ​    ​223.0​, 
 ​    ​196.0​, 
 ​    ​202.0​, 
 ​    ​207.0​, 
 ​    ​197.0​, 
 ​    ​202.0​, 
 ​    ​220.0​, 
 ​    ​348.0​, 
 ​    ​283.0​, 
 ​    ​247.0​, 
 ​    ​245.0​, 
 ​    ​243.0​, 
 ​    ​241.0​, 
 ​    ​239.0​, 
 ​    ​243.0​, 
 ​    ​244.0​, 
 ​    ​245.0​, 
 ​    ​244.0​, 
 ​    ​245.0​, 
 ​    ​245.0​, 
 ​    ​245.0​, 
 ​    ​246.0​, 
 ​    ​246.0​, 
 ​    ​246.0​, 
 ​    ​nothing​, 
 ​    ​nothing​, 
 ​    ​nothing​, 
 ​    ​nothing​, 
 ​    ​nothing​, 
 ​    ​nothing​, 
 ​    ​nothing​, 
 ​    ​nothing​, 
 ​    ​nothing​, 
 ​    ​nothing​, 
 ​    ​nothing​, 
 ​    ​nothing​, 
 ​    ​nothing​, 
 ​    ​nothing​, 
 ​    ​nothing​, 
 ​] 
  
 ​function​ ​_vdw_radius​(atomno) 
 ​     
 ​    radius ​=​ _vdw_radius_crc[atomno] 
 ​     
 ​    ​if​ ​isnothing​(radius)  
 ​        ​return​ ​2.0 
 ​    ​end 
 ​     
 ​    ​return​ radius ​*​ consts​.​centi 
 ​end 
  
 ​""" 
 ​Select reasonable estimates for the van der Waals radii. 
 ​This function returns van der Waals radii as recommended in the 95th 
 ​edition of the "CRC Handbook of Chemistry and Physics" (2014). This 
 ​consists of Bondi radii (A. Bondi. "van der Waals Volumes and Radii". J. 
 ​Phys. Chem. 1964, 68, 3, 441-451. DOI:10.1021/j100785a001) together with 
 ​the values recommended by Truhlar (M. Mantina et al. "Consistent van der 
 ​Waals Radii for the Whole Main Group". J. Phys. Chem. A 2009, 113, 19, 
 ​5806-5812. DOI:10.1021/jp8111556). For hydrogen, the value recommended by 
 ​Taylor is employed (R. Rowland et al. "Intermolecular Nonbonded Contact 
 ​Distances in Organic Crystal Structures: Comparison with Distances Expected 
 ​from van der Waals Radii". J. Phys. Chem. 1996, 100, 18, 7384-7391. 
 ​DOI:10.1021/jp953141+). Other elements receive values recommended by either 
 ​Hu (S.-Z., Hu. Kristallogr. 224, 375, 2009) or Guzei (Guzei, I. A. and 
 ​Wendt, M., Dalton Trans., 2006, 3991, 2006). If neither are defined, we use 
 ​2.0 Å as default. 
 ​Parameters 
 ​---------- 
 ​atomno : array-like 
 ​Returns 
 ​------- 
 ​array-like 
  
 ​Examples 
 ​-------- 
  
 ​```julia-repl 
 ​julia> vdw_radius(1) 
 ​1.1 
 ​``` 
  
 ​```julia-repl 
 ​julia> vdw_radius([1,2,3]) 
 ​1.1 
 ​1.4000000000000001 
 ​1.82 
 ​``` 
  
 ​```julia-repl 
 ​julia> vdw_radius(range(1,3)) 
 ​1.1 
 ​1.4000000000000001 
 ​1.82 
 ​``` 
 ​""" 
 ​@.​ ​function​ ​vdw_radius​(atomno) 
 ​     
 ​    ​return​ ​_vdw_radius​(atomno) 
 ​end

Overloading works as follows

julia> function foo(x)
           println("The most general method")
       end;

julia> function foo(x::AbstractArray)
           println("The array method")
       end;

julia> foo(1)
The most general method

julia> foo([1, 2])
The array method
2 Likes

You probably want something like this there:

julia> const vdw_radius_crc = [ 1.0, 2.0, 3.0, nothing ];

julia> vdw_radius(i::Integer) = isnothing(vdw_radius_crc[i]) ? 2.0 : vdw_radius_crc[i]
vdw_radius (generic function with 1 method)

julia> vdw_radius(a::AbstractVector{<:Integer}) = vdw_radius.(a)
vdw_radius (generic function with 2 methods)

julia> vdw_radius(1)
1.0

julia> vdw_radius([1,2,4])
3-element Vector{Float64}:
 1.0
 2.0
 2.0

julia> vdw_radius(1:3)
3-element Vector{Float64}:
 1.0
 2.0
 3.0

1 Like