Function that accepts scalars or one element vectors/ arrays

ellocco
Mon, 11 Jul 2022 08:40:51 +0000
New to Julia

I would like to restrict a function to accept only argument values which contain only one number, how can this be achieved? My example below accept already scalars, vectors and arrays, but the argument value is not restricted to only one number, any ideas?

function _takeOneNumberInputsOnly(_oneNumber::Union{Vector{<:Number}, Array{<:Number}, Number})
    println("typeof(): ", typeof(_oneNumber), ", \t size(): ", size(_oneNumber))
end

# Example Inputs
oneElementVector = [1.2]
twoElementVector = [1, 2]
oneElementArray = ones(1,1)
scalar_value = 2.3

# This should be fine:
_takeOneNumberInputsOnly(oneElementVector)
_takeOneNumberInputsOnly(scalar_value)
_takeOneNumberInputsOnly(oneElementArray)

# This should throw an error:
_takeOneNumberInputsOnly(twoElementVector)

10 posts - 6 participants

Read full topic